-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopts.go
106 lines (88 loc) · 2.15 KB
/
opts.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
package conf
import "github.com/jessevdk/go-flags"
type confOptions struct {
paths []configPath
args []string
delimiter string
noValidation bool
decoders map[string]DecoderFunc
configFlagOption *flags.Option
flagOpts flags.Options
}
type configPath struct {
path string
optional bool
}
type ConfOption interface {
apply(*confOptions)
}
type ConfOptions []ConfOption
func (opts ConfOptions) apply(o *confOptions) {
for _, opt := range opts {
opt.apply(o)
}
}
type funcConfOption struct {
f func(*confOptions)
}
func (fo *funcConfOption) apply(o *confOptions) {
fo.f(o)
}
func newFuncConfOption(f func(*confOptions)) *funcConfOption {
return &funcConfOption{
f: f,
}
}
func Paths(paths ...string) ConfOption {
return newFuncConfOption(func(o *confOptions) {
for _, path := range paths {
o.paths = append(o.paths, configPath{path: path})
}
})
}
func OptionalPaths(paths ...string) ConfOption {
return newFuncConfOption(func(o *confOptions) {
for _, path := range paths {
o.paths = append(o.paths, configPath{path: path, optional: true})
}
})
}
func ConfigFlag(longName string, paths ...string) ConfOption {
return newFuncConfOption(func(o *confOptions) {
if o.configFlagOption == nil {
o.configFlagOption = &flags.Option{}
}
o.configFlagOption.LongName = longName
o.configFlagOption.Default = paths
})
}
func ConfigFlagOption(configFlagOption *flags.Option) ConfOption {
return newFuncConfOption(func(o *confOptions) {
o.configFlagOption = configFlagOption
})
}
func Delimiter(delimiter string) ConfOption {
return newFuncConfOption(func(o *confOptions) {
o.delimiter = delimiter
})
}
func Args(args []string) ConfOption {
return newFuncConfOption(func(o *confOptions) {
o.args = args
})
}
func NoValidation() ConfOption {
return newFuncConfOption(func(o *confOptions) {
o.noValidation = true
})
}
func AddDecoder(ext string, dec DecoderFunc) ConfOption {
return newFuncConfOption(func(o *confOptions) {
o.decoders[ext] = dec
})
}
func WithFlagOpts(flagOpts flags.Options) ConfOption {
return newFuncConfOption(func(o *confOptions) {
o.flagOpts = flagOpts
})
}