/
config.go
298 lines (246 loc) · 6.86 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package ofa
import (
"fmt"
"strconv"
"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
)
//
// config providers get plugged together to provide a specific setting either
// from configuration, flags or interactive input.
//
type configField interface {
stringValue() (*string, string)
intValue() (*int64, string)
boolValue() (*bool, string)
}
type ConfigProvider func(field string) configField
// return a configured string value
func evaluateString(label string, fields ...configField) *string {
for _, c := range fields {
if value, source := c.stringValue(); value != nil {
if *globalVerbose && len(label) > 0 {
log.Infof("%s %s (%s)", padLabel(label), *value, source)
}
return value
}
}
return nil
}
func evaluateMask(label string, fields ...configField) *string {
for _, c := range fields {
if value, source := c.stringValue(); value != nil {
if *globalVerbose && len(label) > 0 {
log.Infof("%s <configured> (%s)", padLabel(label), source)
}
return value
}
}
return nil
}
// return a configured integer value
func evaluateInt(label string, fields ...configField) *int64 {
for _, c := range fields {
if value, source := c.intValue(); value != nil {
if *globalVerbose && len(label) > 0 {
log.Infof("%s %d (%s)", padLabel(label), *value, source)
}
return value
}
}
return nil
}
// return a configured boolean value
func evaluateBool(label string, fields ...configField) (*bool, string) {
for _, c := range fields {
if value, source := c.boolValue(); value != nil {
if *globalVerbose && len(label) > 0 {
log.Infof("%s %t (%s)", padLabel(label), *value, source)
}
return value, source
}
}
return nil, ""
}
//
// Flag Config gives access to the pflag flags
//
type flagConfig struct {
flags *pflag.FlagSet
flagname string
negate bool
}
func (p *flagConfig) location() string {
return fmt.Sprintf("flag: '%s'", p.flagname)
}
func (p *flagConfig) stringValue() (*string, string) {
if p.flags.Changed(p.flagname) {
return toSPError(p.flags.GetString(p.flagname)), p.location()
}
return nil, ""
}
func (p *flagConfig) intValue() (*int64, string) {
if p.flags.Changed(p.flagname) {
return toIPError(p.flags.GetInt64(p.flagname)), p.location()
}
return nil, ""
}
func (p *flagConfig) boolValue() (*bool, string) {
if p.flags.Changed(p.flagname) {
v, err := p.flags.GetBool(p.flagname)
return toBPError(v != p.negate, err), // this is v ^ p.negate, golang style. Surprisingly annoying
p.location()
}
return nil, ""
}
func newFlagConfig(flags *pflag.FlagSet, negate bool) ConfigProvider {
return func(flagname string) configField {
return &flagConfig{flags, flagname, negate}
}
}
//
// Null Config always return nil for any config key
//
type nullConfig struct {
}
func (p *nullConfig) stringValue() (*string, string) {
return nil, ""
}
func (p *nullConfig) intValue() (*int64, string) {
return nil, ""
}
func (p *nullConfig) boolValue() (*bool, string) {
return nil, ""
}
func newNullConfig() ConfigProvider {
return func(field string) configField {
return &nullConfig{}
}
}
//
// Conditional Config will return values from the wrapped
// config if the condition is true
//
func newConditionalConfig(condition bool, delegate configField) configField {
if condition {
return delegate
} else {
return &nullConfig{}
}
}
//
// Default Config always return a default value
//
type constantConfig struct {
defaultString *string
defaultBool *bool
}
func (p *constantConfig) location() string {
return "default value"
}
func (p *constantConfig) stringValue() (*string, string) {
return p.defaultString, p.location()
}
func (p *constantConfig) intValue() (*int64, string) {
log.Fatal("Not implemented")
return nil, ""
}
func (p *constantConfig) boolValue() (*bool, string) {
return p.defaultBool, p.location()
}
func constantStringValue(defaultString string) configField {
return &constantConfig{defaultString: &defaultString}
}
func constantBoolValue(defaultBool bool) configField {
return &constantConfig{defaultBool: &defaultBool}
}
//
// Prompt Config prompts for user input.
//
type promptConfig struct {
label string
hidden bool
defaultStringValue *string
defaultIntValue *int64
defaultBoolValue *bool
validate promptui.ValidateFunc
}
func (p *promptConfig) location(isDefault bool) string {
if isDefault {
return "user input (default value auto-selected)"
}
return "user input"
}
func (p *promptConfig) stringValue() (*string, string) {
value, err := textInput(p.label, p.defaultStringValue, p.hidden, p.validate)
if err != nil || len(*value) == 0 {
return nil, ""
}
return value, p.location(p.defaultStringValue == value)
}
func (p *promptConfig) intValue() (*int64, string) {
var defaultValue *string = nil
if p.defaultIntValue != nil {
defaultValue = toSP(strconv.FormatInt(*p.defaultIntValue, 10))
}
value, err := textInput(p.label, defaultValue, false, validateNumber)
if err != nil || len(*value) == 0 {
return nil, ""
}
return toIPError(strconv.ParseInt(*value, 10, 64)),
p.location(defaultValue == value) // pointer compare!
}
func (p *promptConfig) boolValue() (*bool, string) {
var defaultValue *string = nil
if p.defaultBoolValue != nil {
defaultValue = toSP(strconv.FormatBool(*p.defaultBoolValue))
}
value, err := textInput(p.label, defaultValue, false, validateBool)
if err != nil || len(*value) == 0 {
return nil, ""
}
return toBPError(strconv.ParseBool(*value)),
p.location(defaultValue == value) // pointer compare!
}
func interactiveStringValue(label string, defaultValue *string, validate promptui.ValidateFunc) configField {
return &promptConfig{label: label, hidden: false, defaultStringValue: defaultValue, validate: validate}
}
func interactiveIntValue(label string, defaultValue *int64) configField {
return &promptConfig{label: label, defaultIntValue: defaultValue}
}
func interactiveBoolValue(label string, defaultValue *bool) configField {
return &promptConfig{label: label, defaultBoolValue: defaultValue}
}
func interactivePasswordValue(label string) configField {
return &promptConfig{label: label, hidden: true}
}
//
// Menu Config provider returns a menu selection
//
type menuConfig struct {
label string
items map[string]*string
defaultValue *string
}
func (p *menuConfig) location() string {
return "user selection"
}
func (p *menuConfig) stringValue() (*string, string) {
res, err := interactiveMenuSelector(p.label, p.items, p.defaultValue)
if err != nil {
return nil, ""
}
return res, p.location()
}
func (p *menuConfig) intValue() (*int64, string) {
log.Fatal("Not implemented")
return nil, ""
}
func (p *menuConfig) boolValue() (*bool, string) {
log.Fatal("Not implemented")
return nil, ""
}
func interactiveMenu(label string, items map[string]*string, defaultValue *string) configField {
return &menuConfig{label, items, defaultValue}
}