-
Notifications
You must be signed in to change notification settings - Fork 2
/
flags_import.go
394 lines (380 loc) · 11.6 KB
/
flags_import.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package nfigure
import (
"flag"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/muir/commonerrors"
"github.com/muir/reflectutils"
"github.com/pkg/errors"
)
type hasIsBool interface {
IsBoolFlag() bool
}
// FlagSet is a subset of what flag.FlagSet supports, defined as
// an interface to lesson the dependency on flag.
type FlagSet interface {
BoolVar(*bool, string, bool, string)
StringVar(*string, string, string, string)
DurationVar(*time.Duration, string, time.Duration, string)
IntVar(*int, string, int, string)
Int64Var(*int64, string, int64, string)
UintVar(*uint, string, uint, string)
Uint64Var(*uint64, string, uint64, string)
Float64Var(*float64, string, float64, string)
Func(string, string, func(string) error)
Parsed() bool
VisitAll(func(*flag.Flag))
}
// ImportFlagSet pulls in flags defined with the standard "flag"
// package. This is useful when there are libaries being used
// that define flags.
//
// flag.CommandLine is the default FlagSet.
//
// ImportFlagSet is not the recommended way to use nfigure, but sometimes
// there is no choice.
func ImportFlagSet(fs FlagSet) FlaghandlerOptArg {
return func(h *FlagHandler) error {
if fs.Parsed() {
return commonerrors.ProgrammerError(errors.New("Cannot import FlagSets that have been parsed"))
}
var err error
fs.VisitAll(func(f *flag.Flag) {
var isBool bool
if hib, ok := f.Value.(hasIsBool); ok {
isBool = hib.IsBoolFlag()
}
ref := &flagRef{
flagTag: flagTag{
Name: []string{f.Name},
},
flagRefComparable: flagRefComparable{
isBool: isBool,
},
imported: f,
}
switch utf8.RuneCountInString(f.Name) {
case 0:
err = commonerrors.ProgrammerError(errors.New("Invalid flag in FlagSet with no Name"))
case 1:
h.shortFlags[f.Name] = ref
default:
h.longFlags[f.Name] = ref
}
h.imported = append(h.imported, ref)
})
return err
}
}
// importFlags deals with setting values for standard "flags" that have been
// imported.
func (h *FlagHandler) importFlags() error {
for _, ref := range h.imported {
switch len(ref.values) {
case 0:
if ref.imported.DefValue != "" {
err := ref.imported.Value.Set(ref.imported.DefValue)
if err != nil {
return commonerrors.ProgrammerError(errors.Errorf("Cannot set default value for flag '%s': %s",
ref.imported.Name, err))
}
}
default:
for _, value := range ref.values {
err := ref.imported.Value.Set(value)
if err != nil {
return commonerrors.UsageError(errors.Errorf("Cannot set value for flag '%s': %s",
ref.imported.Name, err))
}
}
}
}
if h.selectedSubcommand != "" {
return h.subcommands[h.selectedSubcommand].importFlags()
}
return nil
}
// ExportToFlagSet provides a way to use the regular "flag" package to
// when defining flags in a model. This provides a compatibility option for
// library writers so that if nfigure is not the primary configuration system
// for a program, flag setting by libraries is still easy.
//
// flag.CommandLine is the default FlagSet.
//
// Only some of the FlaghandlerOptArgs make sense in this context. The others
// will be ignored.
//
// ExportToFlagSet only exports flags.
//
// Subcommands are not supported by the "flag" package and will be ignored
// by ExportToFlagSet. Counters are not supported by the "flag" package and
// will be treated as numerical types.
//
// If a flag has multiple aliases, only the first name will be used.
func ExportToFlagSet(fs FlagSet, tagName string, model interface{}, opts ...FlaghandlerOptArg) error {
h := GoFlagHandler(opts...)
err := h.PreWalk(tagName, model)
if err != nil {
return err
}
defaultTag := "default"
if h.defaultTag != "" {
defaultTag = h.defaultTag
}
debug("default tag is", defaultTag)
value := reflect.ValueOf(model)
nonPtr := value
for nonPtr.Type().Kind() == reflect.Ptr {
if nonPtr.IsNil() {
return commonerrors.ProgrammerError(errors.New("Must provide a model or pointer to model, not nil"))
}
nonPtr = nonPtr.Elem()
}
for _, f := range h.rawData {
f := f
v := nonPtr.FieldByIndex(f.Index)
tagSet := reflectutils.SplitTag(f.Tag).Set()
tag := tagSet.Get(tagName)
defaultValue, hasDefault := tagSet.Lookup(defaultTag)
ref, setterType, nonPointerType, err := parseFlagRef(tag, f.Type)
if err != nil {
return err
}
setter, err := reflectutils.MakeStringSetter(setterType, reflectutils.WithSplitOn(ref.Split))
if err != nil {
return commonerrors.UsageError(errors.Wrap(err, f.Name))
}
help := tagSet.Get(h.helpTag).Value
if help == "" {
help = fmt.Sprintf("set %s (%s)", f.Name, f.Type)
}
vcopy := v
getV := func() reflect.Value {
return vcopy
}
vt := v.Type()
var isPointer bool
for vt.Kind() == reflect.Ptr {
isPointer = true
current := getV
getV = func() reflect.Value {
v := current()
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
return v.Elem()
}
vt = vt.Elem()
}
if v.Type().Kind() == reflect.Ptr {
c := getV
getV = func() reflect.Value {
v := c()
getV = func() reflect.Value {
return v
}
return v
}
}
debug("flagset, setter type", ref.Name[0], setterType)
switch {
case len(ref.Name) == 0:
return commonerrors.LibraryError(errors.New("missing name"))
case ref.isMap:
ks, err := reflectutils.MakeStringSetter(nonPointerType.Key())
if err != nil {
return commonerrors.ProgrammerError(errors.Wrap(err, ref.used[0]))
}
var once bool
fs.Func(ref.Name[0], help, func(s string) error {
if s == "" {
return commonerrors.UsageError(errors.Errorf("Invalid (empty) value for -%s", ref.Name[0]))
}
v := getV()
if !once {
m := reflect.MakeMap(nonPointerType)
v.Set(m)
once = true
}
vals := strings.SplitN(s, ref.Split, 2)
key := vals[0]
var value string
if len(vals) == 2 {
value = vals[1]
}
debugf("flagfill map %s = %s %s %s", key, value, nonPointerType.Key(), nonPointerType.Elem())
kp := reflect.New(nonPointerType.Key())
err := ks(kp.Elem(), key)
if err != nil {
return commonerrors.UsageError(errors.Wrapf(err, "key for %s", ref.Name[0]))
}
ep := reflect.New(nonPointerType.Elem())
err = setter(ep.Elem(), value)
if err != nil {
return commonerrors.UsageError(errors.Wrapf(err, "value for %s", ref.Name[0]))
}
v.SetMapIndex(reflect.Indirect(kp), reflect.Indirect(ep))
return nil
})
case ref.isSlice:
setElem, err := reflectutils.MakeStringSetter(nonPointerType.Elem())
if err != nil {
return commonerrors.ProgrammerError(errors.Wrap(err, ref.used[0]))
}
switch nonPointerType.Kind() {
case reflect.Array:
index := 0
fs.Func(ref.Name[0], help, func(s string) error {
v := getV()
var values []string
if ref.Split != "" {
values = strings.Split(s, ref.Split)
} else {
values = []string{s}
}
if len(values)+index > v.Len() {
return commonerrors.UsageError(errors.Errorf("overflow array %s", ref.Name[0]))
}
for i, value := range values {
err := setElem(v.Index(i+index), value)
if err != nil {
return commonerrors.UsageError(errors.Wrap(err, ref.Name[0]))
}
}
index += len(values)
return nil
})
case reflect.Slice:
var once bool
fs.Func(ref.Name[0], help, func(s string) error {
v := getV()
var values []string
if ref.Split != "" {
values = strings.Split(s, ref.Split)
} else {
values = []string{s}
}
a := reflect.MakeSlice(nonPointerType, len(values), len(values))
for i, value := range values {
err := setElem(a.Index(i), value)
if err != nil {
return commonerrors.UsageError(errors.Wrap(err, ref.Name[0]))
}
}
if once {
v.Set(reflect.AppendSlice(v, a))
} else {
v.Set(a)
once = true
}
return nil
})
default:
return commonerrors.LibraryError(errors.Errorf("internal error: not expecting %s", v.Type()))
}
case isPointer && !hasDefault:
// For pointers without defaults, there is no point in using one of the flagset
// specific type functions since those require a default and we don't have a
// default. Using one of them would provide a default when instead, nil is
// appropriate.
fs.Func(ref.Name[0], help, func(s string) error {
v := getV()
err := setter(v, s)
return commonerrors.UsageError(errors.Wrap(err, s))
})
case ref.isBool:
v := getV()
var defaultBool bool
if defaultValue.Value != "" {
var err error
defaultBool, err = strconv.ParseBool(defaultValue.Value)
if err != nil {
return commonerrors.ProgrammerError(errors.Wrapf(err, "Parse value of %s tag for default bool", defaultTag))
}
}
fs.BoolVar(v.Addr().Interface().(*bool), ref.Name[0], defaultBool, help)
case setterType == durationType:
v := getV()
var defaultDuration time.Duration
if defaultValue.Value != "" {
var err error
defaultDuration, err = time.ParseDuration(defaultValue.Value)
if err != nil {
return commonerrors.ProgrammerError(errors.Wrapf(err, "Parse value of %s tag for default duration", defaultTag))
}
}
fs.DurationVar(v.Addr().Interface().(*time.Duration), ref.Name[0], defaultDuration, help)
case setterType.Kind() == reflect.String:
v := getV()
fs.StringVar(v.Addr().Interface().(*string), ref.Name[0], defaultValue.Value, help)
case setterType.Kind() == reflect.Int:
v := getV()
var defaultInt int
if defaultValue.Value != "" {
var err error
defaultInt, err = strconv.Atoi(defaultValue.Value)
if err != nil {
return commonerrors.ProgrammerError(errors.Wrapf(err, "Parse value of %s tag for default int", defaultTag))
}
}
fs.IntVar(v.Addr().Interface().(*int), ref.Name[0], defaultInt, help)
case setterType.Kind() == reflect.Int64:
v := getV()
var defaultInt64 int64
if defaultValue.Value != "" {
var err error
defaultInt64, err = strconv.ParseInt(defaultValue.Value, 10, 64)
if err != nil {
return commonerrors.ProgrammerError(errors.Wrapf(err, "Parse value of %s tag for default int", defaultTag))
}
}
fs.Int64Var(v.Addr().Interface().(*int64), ref.Name[0], defaultInt64, help)
case setterType.Kind() == reflect.Uint:
v := getV()
var defaultInt uint64
if defaultValue.Value != "" {
var err error
defaultInt, err = strconv.ParseUint(defaultValue.Value, 10, 32)
if err != nil {
return commonerrors.ProgrammerError(errors.Wrapf(err, "Parse value of %s tag for default int", defaultTag))
}
}
fs.UintVar(v.Addr().Interface().(*uint), ref.Name[0], uint(defaultInt), help)
case setterType.Kind() == reflect.Uint64:
v := getV()
var defaultInt uint64
if defaultValue.Value != "" {
var err error
defaultInt, err = strconv.ParseUint(defaultValue.Value, 10, 64)
if err != nil {
return commonerrors.ProgrammerError(errors.Wrapf(err, "Parse value of %s tag for default int", defaultTag))
}
}
fs.Uint64Var(v.Addr().Interface().(*uint64), ref.Name[0], defaultInt, help)
default:
fs.Func(ref.Name[0], help, func(s string) error {
v := getV()
err := setter(v, s)
return commonerrors.UsageError(errors.Wrap(err, s))
})
}
}
if debugging {
fs.VisitAll(func(f *flag.Flag) {
fmt.Printf("export defined -%s\n", f.Name)
})
}
return nil
}
// MustExportToFlagSet wraps ExportToFlagSet with a panic if the export fails
func MustExportToFlagSet(fs FlagSet, tagName string, model interface{}, opts ...FlaghandlerOptArg) {
err := ExportToFlagSet(fs, tagName, model, opts...)
if err != nil {
panic(err)
}
}
var durationType = reflect.TypeOf(time.Duration(0))