Skip to content

Commit ebee3c5

Browse files
committed
Hello world
1 parent a775ade commit ebee3c5

File tree

8 files changed

+283
-0
lines changed

8 files changed

+283
-0
lines changed

.github/workflows/build.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
name: Build
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Set up Go 1.x
15+
uses: actions/setup-go@v2
16+
with:
17+
go-version: ^1.17
18+
id: go
19+
20+
- name: Set up linter
21+
run: go get -u github.com/mgechev/revive
22+
23+
- name: Check out code into the Go module directory
24+
uses: actions/checkout@v2
25+
26+
- name: Get dependencies
27+
run: go get -v -t -d ./...
28+
29+
- name: Lint
30+
run: revive -config revive.toml -formatter stylish -exclude ./vendor/... ./...
31+
32+
- name: Vet
33+
run: go vet ./...
34+
35+
- name: Test
36+
run: go test -v ./...

.gitignore

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Windows template
3+
# Windows thumbnail cache files
4+
Thumbs.db
5+
Thumbs.db:encryptable
6+
ehthumbs.db
7+
ehthumbs_vista.db
8+
9+
# Dump file
10+
*.stackdump
11+
12+
# IDEA
13+
.idea/
14+
15+
# Folder config file
16+
[Dd]esktop.ini
17+
18+
# Recycle Bin used on file shares
19+
$RECYCLE.BIN/
20+
21+
# Windows Installer files
22+
*.cab
23+
*.msi
24+
*.msix
25+
*.msm
26+
*.msp
27+
28+
# Windows shortcuts
29+
*.lnk
30+
31+
### Linux template
32+
*~
33+
34+
# temporary files which can be created if a process still has a handle open of a deleted file
35+
.fuse_hidden*
36+
37+
# KDE directory preferences
38+
.directory
39+
40+
# Linux trash folder which might appear on any partition or disk
41+
.Trash-*
42+
43+
# .nfs files are created when an open file is removed but is still being accessed
44+
.nfs*
45+
46+
### Go template
47+
# Binaries for programs and plugins
48+
*.exe
49+
*.exe~
50+
*.dll
51+
*.so
52+
*.dylib
53+
54+
# Test binary, built with `go test -c`
55+
*.test
56+
57+
# Output of the go coverage tool, specifically when used with LiteIDE
58+
*.out
59+
60+
# Dependency directories (remove the comment below to include it)
61+
# vendor/
62+
63+
### macOS template
64+
# General
65+
.DS_Store
66+
.AppleDouble
67+
.LSOverride
68+
69+
# Icon must end with two \r
70+
Icon
71+
72+
# Thumbnails
73+
._*
74+
75+
# Files that might appear in the root of a volume
76+
.DocumentRevisions-V100
77+
.fseventsd
78+
.Spotlight-V100
79+
.TemporaryItems
80+
.Trashes
81+
.VolumeIcon.icns
82+
.com.apple.timemachine.donotpresent
83+
84+
# Directories potentially created on remote AFP share
85+
.AppleDB
86+
.AppleDesktop
87+
Network Trash Folder
88+
Temporary Items
89+
.apdisk
90+
91+
bin/
92+
dist/

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
# waitfor-postgres
22
Postgres resource readiness assertion library
3+
4+
# Quick start
5+
6+
```go
7+
package main
8+
9+
import (
10+
"context"
11+
"fmt"
12+
"github.com/go-waitfor/waitfor"
13+
"github.com/go-waitfor/waitfor-postgres"
14+
"os"
15+
)
16+
17+
func main() {
18+
runner := waitfor.New(postgres.Use())
19+
20+
err := runner.Test(
21+
context.Background(),
22+
[]string{"postgres://localhost:8080/my-db"},
23+
waitfor.WithAttempts(5),
24+
)
25+
26+
if err != nil {
27+
fmt.Println(err)
28+
os.Exit(1)
29+
}
30+
}
31+
```

go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module github.com/go-waitfor/waitfor-postgres
2+
3+
go 1.17
4+
5+
require (
6+
github.com/go-waitfor/waitfor v1.0.0
7+
github.com/lib/pq v1.10.4
8+
github.com/stretchr/testify v1.7.0
9+
)
10+
11+
require (
12+
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
13+
github.com/davecgh/go-spew v1.1.0 // indirect
14+
github.com/pmezard/go-difflib v1.0.0 // indirect
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
16+
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
2+
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
3+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
4+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/go-waitfor/waitfor v1.0.0 h1:KX6SpTtEM2OOwJu5QP6MXvdi0Llq55d8dywSSZD+gSI=
6+
github.com/go-waitfor/waitfor v1.0.0/go.mod h1:a5e6B1hss5InR3moU7xAOP2thPsbjbTArD5+Kud4YaQ=
7+
github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk=
8+
github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
9+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
10+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
12+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
13+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
15+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
16+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
17+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

postgres.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package postgres
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"fmt"
7+
"net/url"
8+
"strings"
9+
10+
_ "github.com/lib/pq"
11+
12+
"github.com/go-waitfor/waitfor"
13+
)
14+
15+
const Scheme = "postgres"
16+
17+
type Postgres struct {
18+
url *url.URL
19+
}
20+
21+
func Use() waitfor.ResourceConfig {
22+
return waitfor.ResourceConfig{
23+
Scheme: []string{Scheme},
24+
Factory: New,
25+
}
26+
}
27+
28+
func New(u *url.URL) (waitfor.Resource, error) {
29+
if u == nil {
30+
return nil, fmt.Errorf("%q: %w", "url", waitfor.ErrInvalidArgument)
31+
}
32+
33+
return &Postgres{u}, nil
34+
}
35+
36+
func (s *Postgres) Test(ctx context.Context) error {
37+
db, err := sql.Open(s.url.Scheme, strings.TrimPrefix(s.url.String(), Scheme+"://"))
38+
39+
if err != nil {
40+
return err
41+
}
42+
43+
defer db.Close()
44+
45+
return db.PingContext(ctx)
46+
}

postgres_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package postgres_test
2+
3+
import (
4+
"context"
5+
"github.com/go-waitfor/waitfor"
6+
"github.com/go-waitfor/waitfor-postgres"
7+
"github.com/stretchr/testify/assert"
8+
"testing"
9+
"time"
10+
)
11+
12+
func TestUse(t *testing.T) {
13+
w := waitfor.New(postgres.Use())
14+
15+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
16+
defer cancel()
17+
18+
err := w.Test(ctx, []string{"postgres://usr:pass@localhost/my-db"})
19+
20+
assert.Error(t, err)
21+
}

revive.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
ignoreGeneratedHeader = true
2+
severity = "error"
3+
confidence = 0.8
4+
errorCode = 1
5+
warningCode = 0
6+
7+
[rule.context-as-argument]
8+
[rule.context-keys-type]
9+
[rule.dot-imports]
10+
[rule.error-return]
11+
[rule.error-strings]
12+
[rule.error-naming]
13+
[rule.if-return]
14+
[rule.increment-decrement]
15+
[rule.var-declaration]
16+
[rule.range]
17+
[rule.receiver-naming]
18+
[rule.time-naming]
19+
[rule.unexported-return]
20+
[rule.indent-error-flow]
21+
[rule.errorf]
22+
[rule.empty-block]
23+
[rule.superfluous-else]
24+
[rule.unused-parameter]
25+
[rule.unreachable-code]
26+
[rule.redefines-builtin-id]

0 commit comments

Comments
 (0)