-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflagset.go
200 lines (188 loc) · 5.63 KB
/
flagset.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
package copre
import (
"reflect"
"strings"
"github.com/spf13/pflag"
)
type flagSetOptions struct {
tag string
includeUnchanged bool
nameGetter func([]string) string
}
// FlagSetOption configures how a pflag.FlagSet is used to populate a given structure.
type FlagSetOption interface {
apply(*flagSetOptions)
}
type flagSetOptionAdapter func(*flagSetOptions)
func (c flagSetOptionAdapter) apply(o *flagSetOptions) {
c(o)
}
// IncludeUnchanged will also process the values of unchanged flags. Effectively
// this means the flag defaults, if non zero, will be set as well.
func IncludeUnchanged(f ...bool) FlagSetOption {
return flagSetOptionAdapter(func(o *flagSetOptions) {
v := true
if len(f) > 0 {
v = f[0]
}
o.includeUnchanged = v
})
}
// ComputeFlagName will remove the requirement to explicitly specify the
// flag-name with a tag. For all fields not explicitly tagged, the name
// will be computed based on the path by the provided nameGetter function.
// For example:
// ComputeFlagName(KebabCase)
func ComputeFlagName(nameGetter func([]string) string) FlagSetOption {
return flagSetOptionAdapter(func(o *flagSetOptions) {
o.nameGetter = nameGetter
})
}
// OverrideFlagTag will change the struct-tag used to retrieve the flag name.
// The main purpose of this option is to allow interoperability with libraries
// using the same tag.
//
// However it can also be used to enable edge-cases where multiple flags are used
// to populate the same field.
func OverrideFlagTag(tag string) FlagSetOption {
return flagSetOptionAdapter(func(o *flagSetOptions) {
if tag == "" {
tag = "flag"
}
o.tag = tag
})
}
// FlagSet implements a Loader, that takes a pflag.FlagSet and uses those to
// retrieve configuration values.
//
// Standalone usage example:
// flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
// flags.String("foo-bar", "hello", "")
// err := flags.Parse([]string{})
// // ...
// cfg := struct{ FooBar string }{}
// loader := FlagSet(flags, IncludeUnchanged(), ComputeFlagName(KebabCase))
// err = l.Process(&cfg) // cfg.FooBar will have the default value of the flag "hello"
func FlagSet(flags *pflag.FlagSet, opts ...FlagSetOption) Loader {
o := flagSetOptions{
tag: "flag",
includeUnchanged: false,
nameGetter: func(s []string) string { return "" },
}
for _, opt := range opts {
opt.apply(&o)
}
return LoaderFunc(func(dst interface{}) error {
flagMap := listFlags(flags, o.includeUnchanged)
return StructWalk(dst, func(path []string, field reflect.StructField) (interface{}, error) {
name := o.nameGetter(path)
if tag, ok := field.Tag.Lookup(o.tag); ok {
params := strings.Split(tag, ",")
name = params[0]
}
if name == "" {
return nil, nil
}
if val, ok := flagMap[name]; ok {
// Mismatch is handled by StructWalk
return val, nil
}
return nil, nil
})
})
}
// listFlags will visit all flags of the pflag.FlagSet and return them as map
// with their values.
// If includeUnchanged is true, it will also return unchanged flags and therefore
// their defaults.
func listFlags(flags *pflag.FlagSet, includeUnchanged bool) map[string]interface{} {
flagMap := map[string]interface{}{}
flags.VisitAll(func(flag *pflag.Flag) {
// If includeUnchanged is not set, we have to return and ignore the unchanged flag
if !flag.Changed && !includeUnchanged {
return
}
var v interface{}
switch flag.Value.Type() {
case "bool":
v, _ = flags.GetBool(flag.Name)
case "int":
v, _ = flags.GetInt(flag.Name)
case "intSlice":
v, _ = flags.GetIntSlice(flag.Name)
case "int8":
v, _ = flags.GetInt8(flag.Name)
case "int16":
v, _ = flags.GetInt16(flag.Name)
case "int32":
v, _ = flags.GetInt32(flag.Name)
case "int32Slice":
v, _ = flags.GetInt32Slice(flag.Name)
case "int64":
v, _ = flags.GetInt64(flag.Name)
case "int64Slice":
v, _ = flags.GetInt64Slice(flag.Name)
case "uint":
v, _ = flags.GetUint(flag.Name)
case "uintSlice":
v, _ = flags.GetUintSlice(flag.Name)
case "uint8":
v, _ = flags.GetUint8(flag.Name)
case "uint16":
v, _ = flags.GetUint16(flag.Name)
case "uint32":
v, _ = flags.GetUint32(flag.Name)
case "uint64":
v, _ = flags.GetUint64(flag.Name)
case "float32":
v, _ = flags.GetFloat32(flag.Name)
case "float32Slice":
v, _ = flags.GetFloat32Slice(flag.Name)
case "float64":
v, _ = flags.GetFloat64(flag.Name)
case "float64Slice":
v, _ = flags.GetFloat64Slice(flag.Name)
case "stringSlice":
v, _ = flags.GetStringSlice(flag.Name)
case "stringArray":
v, _ = flags.GetStringArray(flag.Name)
case "stringToInt":
v, _ = flags.GetStringToInt(flag.Name)
case "stringToInt64":
v, _ = flags.GetStringToInt64(flag.Name)
case "stringToString":
v, _ = flags.GetStringToString(flag.Name)
case "bytesBase64":
v, _ = flags.GetBytesBase64(flag.Name)
case "bytesHex":
v, _ = flags.GetBytesHex(flag.Name)
case "count":
v, _ = flags.GetCount(flag.Name)
case "duration":
v, _ = flags.GetDuration(flag.Name)
case "durationSlice":
v, _ = flags.GetDurationSlice(flag.Name)
case "ip":
v, _ = flags.GetIP(flag.Name)
case "ipMask":
v, _ = flags.GetIPv4Mask(flag.Name)
case "ipNet":
v, _ = flags.GetIPNet(flag.Name)
case "ipSlice":
v, _ = flags.GetIPSlice(flag.Name)
default:
v = flag.Value.String()
}
t := reflect.TypeOf(v)
// If the flag has the corresponding zero-type set, do not set it
if t.Comparable() && v == reflect.Zero(t).Interface() {
return
}
// If the flag is an empty slice, do not set it
if t.Kind() == reflect.Slice && reflect.ValueOf(v).Len() == 0 {
return
}
flagMap[flag.Name] = v
})
return flagMap
}