feat: init new populator
commit
510d02d4f4
|
@ -0,0 +1,18 @@
|
||||||
|
# macOS
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
# Window
|
||||||
|
*.exe
|
||||||
|
|
||||||
|
# IntelliJ
|
||||||
|
.idea
|
||||||
|
|
||||||
|
# VSCode
|
||||||
|
.vscode
|
||||||
|
|
||||||
|
# Go
|
||||||
|
coverage.out
|
||||||
|
vendor
|
||||||
|
|
||||||
|
# GoReleaser
|
||||||
|
dist
|
|
@ -0,0 +1,65 @@
|
||||||
|
run:
|
||||||
|
tests: false
|
||||||
|
skip-dirs:
|
||||||
|
- ".*test.*"
|
||||||
|
- ".*mock.*"
|
||||||
|
- ".*generated.*"
|
||||||
|
- ".*example.*"
|
||||||
|
skip-files:
|
||||||
|
- ".*Mock.*"
|
||||||
|
- ".*_mock.*"
|
||||||
|
- ".*_generated.*"
|
||||||
|
|
||||||
|
output:
|
||||||
|
sort-results: true
|
||||||
|
|
||||||
|
linters:
|
||||||
|
disable-all: true
|
||||||
|
enable:
|
||||||
|
- errcheck
|
||||||
|
- gosimple
|
||||||
|
- govet
|
||||||
|
- staticcheck
|
||||||
|
- typecheck
|
||||||
|
- unused
|
||||||
|
- errname
|
||||||
|
- errorlint
|
||||||
|
- execinquery
|
||||||
|
- goerr113
|
||||||
|
- gofumpt
|
||||||
|
- gosec
|
||||||
|
- importas
|
||||||
|
- makezero
|
||||||
|
- nilnil
|
||||||
|
- prealloc
|
||||||
|
- unconvert
|
||||||
|
fast: true
|
||||||
|
|
||||||
|
linters-settings:
|
||||||
|
gosec:
|
||||||
|
excludes:
|
||||||
|
- G402
|
||||||
|
- G501
|
||||||
|
- G505
|
||||||
|
exclude-generated: true
|
||||||
|
govet:
|
||||||
|
check-shadowing: false
|
||||||
|
disable-all: true
|
||||||
|
enable:
|
||||||
|
- assign
|
||||||
|
- atomic
|
||||||
|
- bools
|
||||||
|
- buildtag
|
||||||
|
- composites
|
||||||
|
- copylocks
|
||||||
|
- fieldalignment
|
||||||
|
- httpresponse
|
||||||
|
- loopclosure
|
||||||
|
- lostcancel
|
||||||
|
- nilfunc
|
||||||
|
- printf
|
||||||
|
- unmarshal
|
||||||
|
- unreachable
|
||||||
|
- unusedresult
|
||||||
|
staticcheck:
|
||||||
|
checks: ["all", "-SA1019"]
|
|
@ -0,0 +1,11 @@
|
||||||
|
.PHONY: all test-color lint
|
||||||
|
|
||||||
|
all: test-color lint
|
||||||
|
go mod tidy
|
||||||
|
|
||||||
|
test-color:
|
||||||
|
go install github.com/haunt98/go-test-color@latest
|
||||||
|
go-test-color -race -failfast ./...
|
||||||
|
|
||||||
|
lint:
|
||||||
|
golangci-lint run ./...
|
|
@ -0,0 +1,7 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("Hello, World!")
|
||||||
|
}
|
|
@ -0,0 +1,80 @@
|
||||||
|
module github.com/haunt98/populatedb-go
|
||||||
|
|
||||||
|
go 1.18
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/go-sql-driver/mysql v1.6.0
|
||||||
|
github.com/k1LoW/tbls v1.56.6
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
cloud.google.com/go v0.102.0 // indirect
|
||||||
|
cloud.google.com/go/bigquery v1.25.0 // indirect
|
||||||
|
cloud.google.com/go/compute v1.7.0 // indirect
|
||||||
|
cloud.google.com/go/iam v0.3.0 // indirect
|
||||||
|
cloud.google.com/go/spanner v1.28.0 // indirect
|
||||||
|
github.com/Azure/azure-pipeline-go v0.2.3 // indirect
|
||||||
|
github.com/Azure/azure-storage-blob-go v0.14.0 // indirect
|
||||||
|
github.com/apache/arrow/go/arrow v0.0.0-20211112161151-bc219186db40 // indirect
|
||||||
|
github.com/aquasecurity/go-version v0.0.0-20210121072130-637058cfe492 // indirect
|
||||||
|
github.com/aws/aws-sdk-go v1.42.23 // indirect
|
||||||
|
github.com/aws/aws-sdk-go-v2 v1.11.2 // indirect
|
||||||
|
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.0.0 // indirect
|
||||||
|
github.com/aws/aws-sdk-go-v2/credentials v1.6.4 // indirect
|
||||||
|
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.7.4 // indirect
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2 // indirect
|
||||||
|
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2 // indirect
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.5.0 // indirect
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.5.2 // indirect
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.9.2 // indirect
|
||||||
|
github.com/aws/aws-sdk-go-v2/service/s3 v1.21.0 // indirect
|
||||||
|
github.com/aws/smithy-go v1.9.0 // indirect
|
||||||
|
github.com/fatih/color v1.13.0 // indirect
|
||||||
|
github.com/form3tech-oss/jwt-go v3.2.5+incompatible // indirect
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.0 // indirect
|
||||||
|
github.com/gertd/go-pluralize v0.1.7 // indirect
|
||||||
|
github.com/go-stack/stack v1.8.0 // indirect
|
||||||
|
github.com/goccy/go-yaml v1.9.4 // indirect
|
||||||
|
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||||
|
github.com/golang/protobuf v1.5.2 // indirect
|
||||||
|
github.com/golang/snappy v0.0.3 // indirect
|
||||||
|
github.com/google/flatbuffers v2.0.5+incompatible // indirect
|
||||||
|
github.com/google/go-cmp v0.5.8 // indirect
|
||||||
|
github.com/google/uuid v1.3.0 // indirect
|
||||||
|
github.com/googleapis/enterprise-certificate-proxy v0.1.0 // indirect
|
||||||
|
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
|
||||||
|
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||||
|
github.com/k1LoW/duration v1.1.0 // indirect
|
||||||
|
github.com/k1LoW/expand v0.1.0 // indirect
|
||||||
|
github.com/klauspost/compress v1.13.6 // indirect
|
||||||
|
github.com/lib/pq v1.10.4 // indirect
|
||||||
|
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||||
|
github.com/mattn/go-ieproxy v0.0.1 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||||
|
github.com/minio/pkg v1.1.9 // indirect
|
||||||
|
github.com/pierrec/lz4/v4 v4.1.12 // indirect
|
||||||
|
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
|
||||||
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
github.com/sirupsen/logrus v1.8.1 // indirect
|
||||||
|
github.com/snowflakedb/gosnowflake v1.6.5 // indirect
|
||||||
|
github.com/stretchr/testify v1.8.0 // indirect
|
||||||
|
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||||
|
github.com/xdg-go/scram v1.0.2 // indirect
|
||||||
|
github.com/xdg-go/stringprep v1.0.2 // indirect
|
||||||
|
github.com/xo/dburl v0.9.0 // indirect
|
||||||
|
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
|
||||||
|
go.mongodb.org/mongo-driver v1.8.4 // indirect
|
||||||
|
go.opencensus.io v0.23.0 // indirect
|
||||||
|
golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b // indirect
|
||||||
|
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
|
||||||
|
golang.org/x/oauth2 v0.0.0-20220622183110-fd043fe589d2 // indirect
|
||||||
|
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
|
||||||
|
golang.org/x/text v0.3.7 // indirect
|
||||||
|
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
|
||||||
|
google.golang.org/api v0.86.0 // indirect
|
||||||
|
google.golang.org/appengine v1.6.7 // indirect
|
||||||
|
google.golang.org/genproto v0.0.0-20220624142145-8cd45d7dbd1f // indirect
|
||||||
|
google.golang.org/grpc v1.47.0 // indirect
|
||||||
|
google.golang.org/protobuf v1.28.0 // indirect
|
||||||
|
)
|
|
@ -0,0 +1,69 @@
|
||||||
|
package populatedb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-sql-driver/mysql"
|
||||||
|
tblsconfig "github.com/k1LoW/tbls/config"
|
||||||
|
tblsdatasource "github.com/k1LoW/tbls/datasource"
|
||||||
|
tblsschema "github.com/k1LoW/tbls/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
dialectMySQL = "mysql"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrDialectNotSupport = errors.New("dialect not support ")
|
||||||
|
|
||||||
|
type Populator interface{}
|
||||||
|
|
||||||
|
type populator struct {
|
||||||
|
db *sql.DB
|
||||||
|
tblsSchema *tblsschema.Schema
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPopulator(dbDialect, dbURL string) (Populator, error) {
|
||||||
|
if dbDialect != dialectMySQL {
|
||||||
|
return nil, fmt.Errorf("not support [%s]: %w", dbDialect, ErrDialectNotSupport)
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://go.dev/doc/tutorial/database-access
|
||||||
|
mysqlCfg, err := mysql.ParseDSN(dbURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("mysql: failed to parse dsn [%s]: %w", dbURL, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://github.com/go-sql-driver/mysql#timetime-support
|
||||||
|
mysqlCfg.ParseTime = true
|
||||||
|
mysqlCfg.AllowNativePasswords = true
|
||||||
|
mysqlCfg.Loc = time.UTC
|
||||||
|
|
||||||
|
mysqlURL := mysqlCfg.FormatDSN()
|
||||||
|
db, err := sql.Open(dbDialect, mysqlURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("sql: failed to open [%s]: %w", mysqlURL, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.Ping(); err != nil {
|
||||||
|
return nil, fmt.Errorf("database: failed to ping [%s] : %w", mysqlURL, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://github.com/k1LoW/tbls
|
||||||
|
// https://stackoverflow.com/q/48671938
|
||||||
|
tblsURL := "mysql://" + mysqlCfg.User + ":" + url.QueryEscape(mysqlCfg.Passwd) + "@" + mysqlCfg.Addr + "/" + mysqlCfg.DBName
|
||||||
|
tblsSchema, err := tblsdatasource.Analyze(tblsconfig.DSN{
|
||||||
|
URL: tblsURL,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("tbls: faield to analyze [%s]: %w", tblsURL, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &populator{
|
||||||
|
db: db,
|
||||||
|
tblsSchema: tblsSchema,
|
||||||
|
}, nil
|
||||||
|
}
|
Loading…
Reference in New Issue