-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.go
156 lines (123 loc) · 3.44 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package psqldocker
import (
"time"
"github.com/ory/dockertest/v3"
)
type options struct {
containerName,
imageTag,
poolEndpoint,
repository,
dbPort string
sqls []string
pool *dockertest.Pool
expirationSeconds uint
pingRetryTimeout time.Duration
}
func defaultOptions() options {
return options{
containerName: "go-psqldocker",
imageTag: "alpine",
repository: "postgres",
poolEndpoint: "",
dbPort: "5432",
sqls: nil,
pool: nil,
expirationSeconds: 20,
pingRetryTimeout: 20 * time.Second,
}
}
// Option configures an BTC Node Docker.
type Option interface {
apply(*options)
}
type containerNameOption string
func (c containerNameOption) apply(opts *options) {
opts.containerName = string(c)
}
// WithContainerName configures the PSQL Container Name, if
// empty, a random one will be picked.
func WithContainerName(name string) Option {
return containerNameOption(name)
}
type imageTagOption string
func (t imageTagOption) apply(opts *options) {
opts.imageTag = string(t)
}
// WithImageTag configures the PSQL Container image tag, default: alpine.
func WithImageTag(tag string) Option {
return imageTagOption(tag)
}
type useTimescaleDBOption bool
func (useTimescaleDBOption) apply(opts *options) {
opts.repository = "timescale/timescaledb"
// Set to a valid image tag, but don't override unless it is the
// default.
if opts.imageTag == "alpine" {
opts.imageTag = "latest-pg15"
}
}
// WithTimescaleDB allows using the TimescaleDB repository and
// images. This option also updates the image tag to 'latest-pg15' as
// the default alpine is invalid.
func WithTimescaleDB() Option {
return useTimescaleDBOption(true)
}
type sqlOption string
func (c sqlOption) apply(opts *options) {
opts.sqls = append(opts.sqls, string(c))
}
// WithSQL specifies a sqls file, to initiate the
// db with.
func WithSQL(sql string) Option {
return sqlOption(sql)
}
type dbPortOption string
func (c dbPortOption) apply(opts *options) {
opts.dbPort = string(c)
}
// WithDBPort sets database port running in the container, default 5432.
func WithDBPort(name string) Option {
return dbPortOption(name)
}
type poolOption struct {
p *dockertest.Pool
}
func (p poolOption) apply(opts *options) {
opts.pool = p.p
}
// WithPool sets the docker container getPool.
// ! This is mutually exclusive with WithPoolEndpoint, and an error
// will be thrown if both are used.
func WithPool(pool *dockertest.Pool) Option {
return poolOption{pool}
}
type poolEndpoint struct {
e string
}
func (p poolEndpoint) apply(opts *options) {
opts.poolEndpoint = p.e
}
// WithPoolEndpoint sets the docker container pool endpoint.
// ! This is mutually exclusive with WithPool, and an error
// will be thrown if both are used.
func WithPoolEndpoint(endpoint string) Option {
return poolEndpoint{endpoint}
}
type pingRetryTimeout time.Duration
func (p pingRetryTimeout) apply(opts *options) {
opts.pingRetryTimeout = time.Duration(p)
}
// WithPingRetryTimeout sets the timeout in seconds
// for the ping retry function.
func WithPingRetryTimeout(seconds uint) Option {
return pingRetryTimeout(time.Duration(seconds) * time.Second)
}
type expirationSeconds uint
func (e expirationSeconds) apply(opts *options) {
opts.expirationSeconds = uint(e)
}
// WithExpiration terminates the container after a period has passed.
func WithExpiration(seconds uint) Option {
return expirationSeconds(seconds)
}