-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathentry.go
247 lines (210 loc) · 6.56 KB
/
entry.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
package config
import (
"fmt"
"reflect"
"strings"
"time"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// Entry is one item to define a configuration
type Entry struct {
name string
usage string
defaultValue interface{}
desiredType reflect.Type
bindFlag bool
flagShortName string
bindEnv bool
}
// EntryOption represents an option for the Entry
type EntryOption func(e *Entry)
// Default specifies a default value
func Default(value interface{}) EntryOption {
return func(e *Entry) {
e.defaultValue = value
}
}
// ShortName specifies the shorthand (one-letter) flag name
func ShortName(fShort string) EntryOption {
return func(e *Entry) {
e.flagShortName = fShort
}
}
// Bind enables/ disables binding of flag and env var
func Bind(flag, env bool) EntryOption {
return func(e *Entry) {
e.bindFlag = flag
e.bindEnv = env
}
}
// DesiredType sets the desired type of this entry
func DesiredType(t reflect.Type) EntryOption {
return func(e *Entry) {
e.desiredType = t
}
}
// NewEntry creates a new Entry that is available as flag, config file entry and environment variable
func NewEntry(name, usage string, options ...EntryOption) Entry {
entry := Entry{
name: name,
usage: usage,
flagShortName: "",
defaultValue: nil,
bindFlag: true,
bindEnv: true,
desiredType: nil,
}
// apply the options
for _, opt := range options {
opt(&entry)
}
// try the best to deduce the desired type
if entry.desiredType == nil {
if entry.defaultValue != nil {
entry.desiredType = reflect.TypeOf(entry.defaultValue)
} else {
entry.desiredType = reflect.TypeOf("")
}
}
return entry
}
func (e Entry) String() string {
return fmt.Sprintf("--%s (-%s) [default:%v (%T)]\t- %s", e.name, e.flagShortName, e.defaultValue, e.defaultValue, e.usage)
}
// Name provides the specified name for this entry
func (e Entry) Name() string {
return e.name
}
// IsRequired returns true in case no default value is given
func (e Entry) IsRequired() bool {
return e.defaultValue == nil
}
// EnvVarName returns the name of the environment variable for this entry.
func (e Entry) EnvVarName(envPrefix string) string {
envVarName := fmt.Sprintf("%s_%s", envPrefix, e.Name())
envVarName = envVarReplacer.Replace(envVarName)
envVarName = strings.ToUpper(envVarName)
return envVarName
}
func checkViper(vp *viper.Viper, entry Entry) error {
if vp == nil {
return fmt.Errorf("Viper is nil")
}
if len(entry.name) == 0 {
return fmt.Errorf("Name is missing")
}
return nil
}
func registerFlag(flagSet *pflag.FlagSet, entry Entry) error {
if !entry.bindFlag {
return nil
}
if flagSet == nil {
return fmt.Errorf("FlagSet is nil")
}
if len(entry.name) == 0 {
return fmt.Errorf("Name is missing")
}
valueDesiredType := entry.defaultValue
if valueDesiredType == nil {
// For struct types ensure that the input type is treated as string.
// Because it is not possible to register a flag for complex types such as structs.
if entry.desiredType.Kind() == reflect.Struct {
valueDesiredType = ""
} else {
// Use the desired type as type for registering the flag if its a primitive type.
valueType := reflect.New(entry.desiredType)
if valueType.Kind() != reflect.Ptr {
return fmt.Errorf("Failed deducing desired type for entry %v", entry)
}
valueDesiredType = valueType.Elem().Interface()
}
}
switch castedDefaultValue := valueDesiredType.(type) {
case string:
flagSet.StringP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case uint:
flagSet.UintP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case int:
flagSet.IntP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case bool:
flagSet.BoolP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case time.Duration:
flagSet.DurationP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case float32:
flagSet.Float32P(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case float64:
flagSet.Float64P(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case []bool:
flagSet.BoolSliceP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case []string:
flagSet.StringSliceP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case []time.Duration:
flagSet.DurationSliceP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case []int:
flagSet.IntSliceP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case []int32:
flagSet.Int32SliceP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case []int64:
flagSet.Int64SliceP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case []uint:
flagSet.UintSliceP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case []float64:
flagSet.Float64SliceP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
case []float32:
flagSet.Float32SliceP(entry.name, entry.flagShortName, castedDefaultValue, entry.usage)
default:
typeOfDefaultValue := reflect.TypeOf(castedDefaultValue)
if typeOfDefaultValue.Kind() != reflect.Slice {
return fmt.Errorf("Type %s is not yet supported", typeOfDefaultValue)
}
// this part supports slices of custom structs and registers the according flag for it
s := sliceOfMapStringToInterfaceFlag{}
flagSet.VarP(&s, entry.name, entry.flagShortName, entry.usage)
return nil
}
return nil
}
func setDefault(vp *viper.Viper, entry Entry) error {
if err := checkViper(vp, entry); err != nil {
return err
}
if entry.defaultValue != nil {
vp.SetDefault(entry.name, entry.defaultValue)
}
return nil
}
func registerEnv(vp *viper.Viper, envPrefix string, entry Entry) error {
if !entry.bindEnv {
return nil
}
if err := checkViper(vp, entry); err != nil {
return err
}
if len(envPrefix) > 0 {
vp.SetEnvPrefix(envPrefix)
}
return vp.BindEnv(entry.name)
}
// sliceOfMapStringToInterfaceFlag is a struct that can be used to represent a flag of type
//
// []map[string]interface{}
//
// That is a slice of arbitrary structs.
type sliceOfMapStringToInterfaceFlag struct {
// used to be returned in the String method. Its better to return the value as
// json string since this can be parsed easier if needed.
jsonSting string
}
func (l *sliceOfMapStringToInterfaceFlag) String() string {
return l.jsonSting
}
func (l *sliceOfMapStringToInterfaceFlag) Type() string {
return "[]map[string]interface{}"
}
func (l *sliceOfMapStringToInterfaceFlag) Set(in string) error {
in = strings.ReplaceAll(in, "'", "\"")
l.jsonSting = in
return nil
}