-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
53 lines (44 loc) · 1.04 KB
/
config.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
package daemon
type config struct {
args []string
envs map[string]string
outputChan chan<- string
}
func newConfig(opts []Option) (config, error) {
var cfg config
for _, opt := range opts {
opt.apply(&cfg)
}
return cfg, nil
}
// Option configures a Daemon.
type Option interface {
apply(*config)
}
type funcOption struct {
f func(*config)
}
func newFuncOption(f func(*config)) *funcOption {
return &funcOption{f: f}
}
func (fo *funcOption) apply(cfg *config) {
fo.f(cfg)
}
// WithArguments sets command line arguments for a daemon.
func WithArguments(args ...string) Option {
return newFuncOption(func(cfg *config) {
cfg.args = args
})
}
// WithEnvironments sets environment variables for a daemon.
func WithEnvironments(envs map[string]string) Option {
return newFuncOption(func(cfg *config) {
cfg.envs = envs
})
}
// WithOutputChannel sets a channel to receive stdout and stderr.
func WithOutputChannel(outputChan chan<- string) Option {
return newFuncOption(func(cfg *config) {
cfg.outputChan = outputChan
})
}