/
config.go
280 lines (267 loc) · 8.25 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
// Package config provides flexible access to config variables by priority:
// flags - HI,
// environment variables - MID,
// default values defined with a struct field tags - LOW
package config
import (
"errors"
"flag"
"fmt"
"os"
"reflect"
"strconv"
"strings"
"time"
)
var (
// receiver is not a pointer to a struct or a nil pointer
errInvalidReceiver = errors.New("The argument to Init() func must be a non-nil pointer to a struct")
// value is not addressable and or obtained by the use of unexported struct fields
errCantSet = errors.New("Value can not be set")
// trying to assign the value to unsupported field type
errUnsupportedType = func(typeName string) error {
return fmt.Errorf("Unsupported type [%s] in config", typeName)
}
// value parse error
errCantUse = func(val string, typ interface{}) error {
return fmt.Errorf("cannot use [%s] as type [%T]", val, typ)
}
// missing required argument/flag
errMissingRequired = func(name string) error {
return fmt.Errorf("missing required [--%s] argument/flag", name)
}
)
// private constants
const (
// keyDefaultTag - tag name for default value
keyDefaultTag = "default"
// keyIsRequired shold have any non-empty value if variable is required
keyIsRequired = "required"
// keyEnvVar - tag name for env variable name
keyEnvTag = "env"
// keyFlagTag - tag name for variable flag.
// By default (if there is no tag "flag" for struct field) will have name:
// -structname-nestedstructname-varname
keyFlagTag = "keyFlag"
// constant for internal use
emptyPrefix = ""
// comma separator
comma = ","
// space symbol
space = " "
)
// EnvPrefix is a prefix in the beginning of environment variable name (used to
// easily differentiate variables of your application).
var EnvPrefix string
// command line arguments
var args = os.Args[1:]
// required args/flags container
var seen map[string]bool
// Init config values.
func Init(c interface{}, prefix string) error {
// check argument type (only pointer to struct is supported)
rv := reflect.ValueOf(c)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return errInvalidReceiver
}
// set custom "application" prefix (will be used to build ENV VAR names)
EnvPrefix = prefix
// init flagset
flagSet := NewFlagSet("config", flag.ContinueOnError)
// reset required list
seen = make(map[string]bool)
if err := initConfig(rv, flagSet, emptyPrefix); err != nil {
return err
}
// parse flags
if err := flagSet.Parse(args); err != nil {
return err
}
// mark as seen flags that have been set
flagSet.Visit(func(f *flag.Flag) { seen[f.Name] = true })
// find missing required values
for flagName, ok := range seen {
if !ok {
return errMissingRequired(flagName)
}
}
// success
return nil
}
// initConfig recursively loads parameters to Config struct, supports nested
// anonymous structs.
func initConfig(c reflect.Value, flagSet *FlagSet, prefix string) error {
c = reflect.Indirect(c)
if c.Kind() != reflect.Struct {
return errInvalidReceiver
}
for i := 0; i < c.NumField(); i++ {
var value string
field := c.Field(i)
if field.Kind() == reflect.Struct {
np := nestedPrefix(prefix, c.Type().Field(i).Name)
err := initConfig(field.Addr(), flagSet, np)
if err != nil {
return err
}
continue
}
if !field.CanSet() {
return errCantSet
}
structField := c.Type().Field(i)
flgKey := flagName(structField, prefix)
// read "is required" field tag
if isRequired := structField.Tag.Get(keyIsRequired); isRequired != "" {
// init map cell with flgKey (set false because it was not seen yet)
seen[flgKey] = false
}
// getting value from "default" tag
defValue := structField.Tag.Get(keyDefaultTag)
if defValue != "" {
value = defValue
seen[flgKey] = true
}
// retrieve value from ENV variable
envValue := os.Getenv(envName(structField, prefix))
if envValue != "" {
value = envValue
seen[flgKey] = true
}
// set value with a flag
err := setValue(field, flagSet, flgKey, value)
if err != nil {
return err
}
}
return nil
}
// setValue casts string value and assigns it to the field of Config struct.
func setValue(field reflect.Value, flagSet *FlagSet, flgKey, value string) error {
switch t := field.Interface().(type) {
case time.Duration:
val, err := time.ParseDuration(value)
if err != nil {
return errCantUse(value, t)
}
flagSet.DurationVar(field.Addr().Interface().(*time.Duration), flgKey, val, "")
case []time.Duration:
arrDuration := new(arrayDuration)
if err := arrDuration.Set(value); err != nil {
return err
}
flagSet.ArrayDurationVar(field.Addr().Interface().(*[]time.Duration), flgKey, []time.Duration(*arrDuration), "")
case int:
val, err := strconv.Atoi(value)
if err != nil && value != "" {
return errCantUse(value, t)
}
flagSet.IntVar(field.Addr().Interface().(*int), flgKey, val, "")
case []int:
arrInt := new(arrayInt)
if err := arrInt.Set(value); err != nil {
return err
}
flagSet.ArrayIntVar(field.Addr().Interface().(*[]int), flgKey, []int(*arrInt), "")
case int64:
val, err := strconv.ParseInt(value, 10, 64)
if err != nil && value != "" {
return errCantUse(value, t)
}
flagSet.Int64Var(field.Addr().Interface().(*int64), flgKey, val, "")
case []int64:
arrInt64 := new(arrayInt64)
if err := arrInt64.Set(value); err != nil {
return err
}
flagSet.ArrayInt64Var(field.Addr().Interface().(*[]int64), flgKey, []int64(*arrInt64), "")
case uint:
val, err := strconv.ParseUint(value, 10, 64)
if err != nil && value != "" {
return errCantUse(value, t)
}
flagSet.UintVar(field.Addr().Interface().(*uint), flgKey, uint(val), "")
case []uint:
arrUint := new(arrayUint)
if err := arrUint.Set(value); err != nil {
return err
}
flagSet.ArrayUintVar(field.Addr().Interface().(*[]uint), flgKey, []uint(*arrUint), "")
case uint64:
val, err := strconv.ParseUint(value, 10, 64)
if err != nil && value != "" {
return errCantUse(value, t)
}
flagSet.Uint64Var(field.Addr().Interface().(*uint64), flgKey, val, "")
case []uint64:
arrUint64 := new(arrayUint64)
if err := arrUint64.Set(value); err != nil {
return err
}
flagSet.ArrayUint64Var(field.Addr().Interface().(*[]uint64), flgKey, []uint64(*arrUint64), "")
case float64:
val, err := strconv.ParseFloat(value, 64)
if err != nil && value != "" {
return errCantUse(value, t)
}
flagSet.Float64Var(field.Addr().Interface().(*float64), flgKey, val, "")
case []float64:
arrFloat64 := new(arrayFloat64)
if err := arrFloat64.Set(value); err != nil {
return err
}
flagSet.ArrayFloat64Var(field.Addr().Interface().(*[]float64), flgKey, []float64(*arrFloat64), "")
case string:
flagSet.StringVar(field.Addr().Interface().(*string), flgKey, value, "")
case []string:
flagSet.ArrayStringVar(field.Addr().Interface().(*[]string), flgKey, strings.Split(value, comma), "")
case bool:
val, err := strconv.ParseBool(value)
if err != nil {
val = false
}
flagSet.BoolVar(field.Addr().Interface().(*bool), flgKey, val, "")
default:
return errUnsupportedType(field.Kind().String())
}
return nil
}
// nestedPrefix concats prefix for generating default flag names and env variable names.
func nestedPrefix(base, newPrefix string) string {
if base == "" {
return newPrefix
}
return base + " " + newPrefix
}
// envName gets environment variable name for passed field based on provided
// struct tags or default rules (ENVPREFIX_STRUCTNAME_NESTEDSTRUCTNAME_VARNAME).
func envName(field reflect.StructField, prefix string) string {
tag := field.Tag.Get(keyEnvTag)
if tag != "" {
return tag
}
s := joinStrings("_", EnvPrefix, prefix, field.Name)
return strings.ToUpper(s)
}
// flagName gets flag name for passed field based on provided struct tags or
// default rules (-structname-nestedstructname-varname).
func flagName(field reflect.StructField, prefix string) string {
tag := field.Tag.Get(keyFlagTag)
if tag != "" {
return tag
}
s := joinStrings("-", prefix, field.Name)
return strings.ToLower(s)
}
// joinStrings similar to strings.Join, but omits empty values, also replaces
// spaces with provided separator.
func joinStrings(sep string, parts ...string) string {
var components []string
for _, part := range parts {
if part != "" {
components = append(components, part)
}
}
joined := strings.Join(components, sep)
return strings.Replace(joined, " ", sep, -1)
}