From ebee3c50e9b076b67f33192ced5987cf75d58dbf Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Fri, 10 Dec 2021 16:58:32 -0500 Subject: [PATCH] Hello world --- .github/workflows/build.yml | 36 +++++++++++++++ .gitignore | 92 +++++++++++++++++++++++++++++++++++++ README.md | 29 ++++++++++++ go.mod | 16 +++++++ go.sum | 17 +++++++ postgres.go | 46 +++++++++++++++++++ postgres_test.go | 21 +++++++++ revive.toml | 26 +++++++++++ 8 files changed, 283 insertions(+) create mode 100644 .github/workflows/build.yml create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 postgres.go create mode 100644 postgres_test.go create mode 100644 revive.toml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..0c8b086 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,36 @@ +name: Build + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Set up Go 1.x + uses: actions/setup-go@v2 + with: + go-version: ^1.17 + id: go + + - name: Set up linter + run: go get -u github.com/mgechev/revive + + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + + - name: Get dependencies + run: go get -v -t -d ./... + + - name: Lint + run: revive -config revive.toml -formatter stylish -exclude ./vendor/... ./... + + - name: Vet + run: go vet ./... + + - name: Test + run: go test -v ./... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..106f21a --- /dev/null +++ b/.gitignore @@ -0,0 +1,92 @@ +# Created by .ignore support plugin (hsz.mobi) +### Windows template +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# IDEA +.idea/ + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +### Linux template +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### Go template +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +### macOS template +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +bin/ +dist/ \ No newline at end of file diff --git a/README.md b/README.md index e9d70f9..5367ccd 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,31 @@ # waitfor-postgres Postgres resource readiness assertion library + +# Quick start + +```go +package main + +import ( + "context" + "fmt" + "github.com/go-waitfor/waitfor" + "github.com/go-waitfor/waitfor-postgres" + "os" +) + +func main() { + runner := waitfor.New(postgres.Use()) + + err := runner.Test( + context.Background(), + []string{"postgres://localhost:8080/my-db"}, + waitfor.WithAttempts(5), + ) + + if err != nil { + fmt.Println(err) + os.Exit(1) + } +} +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..649f8a0 --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module github.com/go-waitfor/waitfor-postgres + +go 1.17 + +require ( + github.com/go-waitfor/waitfor v1.0.0 + github.com/lib/pq v1.10.4 + github.com/stretchr/testify v1.7.0 +) + +require ( + github.com/cenkalti/backoff v2.2.1+incompatible // indirect + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d27a6fb --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= +github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-waitfor/waitfor v1.0.0 h1:KX6SpTtEM2OOwJu5QP6MXvdi0Llq55d8dywSSZD+gSI= +github.com/go-waitfor/waitfor v1.0.0/go.mod h1:a5e6B1hss5InR3moU7xAOP2thPsbjbTArD5+Kud4YaQ= +github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk= +github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/postgres.go b/postgres.go new file mode 100644 index 0000000..e1e6c65 --- /dev/null +++ b/postgres.go @@ -0,0 +1,46 @@ +package postgres + +import ( + "context" + "database/sql" + "fmt" + "net/url" + "strings" + + _ "github.com/lib/pq" + + "github.com/go-waitfor/waitfor" +) + +const Scheme = "postgres" + +type Postgres struct { + url *url.URL +} + +func Use() waitfor.ResourceConfig { + return waitfor.ResourceConfig{ + Scheme: []string{Scheme}, + Factory: New, + } +} + +func New(u *url.URL) (waitfor.Resource, error) { + if u == nil { + return nil, fmt.Errorf("%q: %w", "url", waitfor.ErrInvalidArgument) + } + + return &Postgres{u}, nil +} + +func (s *Postgres) Test(ctx context.Context) error { + db, err := sql.Open(s.url.Scheme, strings.TrimPrefix(s.url.String(), Scheme+"://")) + + if err != nil { + return err + } + + defer db.Close() + + return db.PingContext(ctx) +} diff --git a/postgres_test.go b/postgres_test.go new file mode 100644 index 0000000..e19a595 --- /dev/null +++ b/postgres_test.go @@ -0,0 +1,21 @@ +package postgres_test + +import ( + "context" + "github.com/go-waitfor/waitfor" + "github.com/go-waitfor/waitfor-postgres" + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +func TestUse(t *testing.T) { + w := waitfor.New(postgres.Use()) + + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + + err := w.Test(ctx, []string{"postgres://usr:pass@localhost/my-db"}) + + assert.Error(t, err) +} diff --git a/revive.toml b/revive.toml new file mode 100644 index 0000000..8774472 --- /dev/null +++ b/revive.toml @@ -0,0 +1,26 @@ +ignoreGeneratedHeader = true +severity = "error" +confidence = 0.8 +errorCode = 1 +warningCode = 0 + +[rule.context-as-argument] +[rule.context-keys-type] +[rule.dot-imports] +[rule.error-return] +[rule.error-strings] +[rule.error-naming] +[rule.if-return] +[rule.increment-decrement] +[rule.var-declaration] +[rule.range] +[rule.receiver-naming] +[rule.time-naming] +[rule.unexported-return] +[rule.indent-error-flow] +[rule.errorf] +[rule.empty-block] +[rule.superfluous-else] +[rule.unused-parameter] +[rule.unreachable-code] +[rule.redefines-builtin-id] \ No newline at end of file