/
options.go
109 lines (93 loc) · 2.43 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
package docker
type containerVolumes struct {
licenseVolumeEnabled, sourceCodeVolumeEnabled bool
licenseVolumeHost, sourceCodeVolumeHost string
}
type containerPorts struct {
webPortEnabled bool
webPortHost int
}
type RunImageOption func(opts *runImageHandler)
type runImageHandler struct {
args []string
volumes containerVolumes
ports containerPorts
setupInterrupt bool
spawnWebBrowser bool
progressLoader bool
duringLoadMessages []string
afterLoadMessages []string
attachOutput bool
exitErrorMessages []string
}
func newRunImageHandler(opts []RunImageOption) runImageHandler {
// defaults here
rh := runImageHandler{}
for _, opt := range opts {
opt(&rh)
}
return rh
}
// Prepend option functions with "Option"
func OptionWithArgs(args []string) RunImageOption {
return func(rh *runImageHandler) {
rh.args = args
}
}
func OptionWithLicenseVolume(volumeHost string) RunImageOption {
return func(rh *runImageHandler) {
rh.volumes.licenseVolumeEnabled = true
rh.volumes.licenseVolumeHost = volumeHost
}
}
func OptionWithSourceVolume(volumeHost string) RunImageOption {
return func(rh *runImageHandler) {
rh.volumes.sourceCodeVolumeEnabled = true
rh.volumes.sourceCodeVolumeHost = volumeHost
}
}
func OptionWithWebPort(portHost int) RunImageOption {
return func(rh *runImageHandler) {
rh.ports.webPortEnabled = true
rh.ports.webPortHost = portHost
}
}
func OptionWithInterrupt() RunImageOption {
return func(rh *runImageHandler) {
rh.setupInterrupt = true
}
}
func OptionWithAutoSpawnBrowser() RunImageOption {
return func(rh *runImageHandler) {
rh.spawnWebBrowser = true
}
}
func OptionWithProgressLoader(duringLoadMessages []string, afterLoadMessages []string) RunImageOption {
return func(rh *runImageHandler) {
rh.progressLoader = true
if len(duringLoadMessages) > 0 {
rh.duringLoadMessages = duringLoadMessages
}
if len(afterLoadMessages) > 0 {
rh.afterLoadMessages = afterLoadMessages
}
}
}
func OptionWithExitErrorMessages(messages []string) RunImageOption {
return func(rh *runImageHandler) {
rh.exitErrorMessages = messages
}
}
func OptionWithAttachedOutput() RunImageOption {
return func(rh *runImageHandler) {
rh.attachOutput = true
}
}
func OptionWithDebug(isDebug bool) RunImageOption {
return func(rh *runImageHandler) {
// currently only enable output in debug mode
if isDebug {
rh.attachOutput = true
}
}
}