-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
103 lines (85 loc) · 2.09 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
package dft
import "strings"
type containerCfg struct {
args *[]string
env *[]string
mounts *[][2]string
ports *[][2]uint
}
type waitCfg struct {
// inContainer indicates if we need to execute the cmd
// inside of the container (true) or on the host (false)
//
// default: false
inContainer *bool
}
type (
ContainerOption func(cfg *containerCfg)
WaitOption func(cfg *waitCfg)
)
// WithCmd overwrites the [CMD] part of the dockerfile
func WithCmd(args []string) ContainerOption {
return func(cfg *containerCfg) {
if cfg.args == nil {
cfg.args = new([]string)
}
n := append(
*cfg.args,
args...,
)
cfg.args = &n
}
}
// WithEnvVar will add "<KEY>=<VALUE>" to the env of the container
func WithEnvVar(key string, value string) ContainerOption {
return func(cfg *containerCfg) {
if cfg.env == nil {
cfg.env = new([]string)
}
n := append(
*cfg.env,
strings.ToUpper(key)+"="+value,
)
cfg.env = &n
}
}
// WithExecuteInsideContainer defines if the wait cmd is executed inside the container
// or on the host machine
func WithMount(dest string, trgt string) ContainerOption {
return func(cfg *containerCfg) {
if cfg.mounts == nil {
cfg.mounts = &[][2]string{{dest, trgt}}
return
}
*cfg.mounts = append(*cfg.mounts, [2]string{dest, trgt})
}
}
// WithPort will expose the passed internal port via a given target port on the host.
func WithPort(port uint, target uint) ContainerOption {
return func(cfg *containerCfg) {
if cfg.ports == nil {
cfg.ports = new([][2]uint)
}
n := append(*cfg.ports, [2]uint{port, target})
cfg.ports = &n
}
}
// WithRandomPort will expose the passed internal port via a random port on the host.
// Use
//
// ExposedPort
// ExposedPortAddr
//
// to retrieve the actual host port used
//
// (shorthand for `WithPort(x,0)`)
func WithRandomPort(port uint) ContainerOption {
return WithPort(port, 0)
}
// WithExecuteInsideContainer defines if the wait cmd is executed inside the container
// or on the host machine
func WithExecuteInsideContainer(b bool) WaitOption {
return func(cfg *waitCfg) {
cfg.inContainer = &b
}
}