-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfigurator.go
368 lines (306 loc) · 9.87 KB
/
configurator.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
// Package configurator implements simple configuration built on top of Viper.
//
// It aims to make application configuration as easy as possible.
// This is accomplish by allowing you to annotate your struct with env, file,
// flag, default annotations that will tell Viper where to where to look for
// configuration values. Since configurator is built on top of Viper the
// same source precedence is applicable.
//
// The priority of config sources is the following:
// 1. Overrides, or setting the config struct field directly.
// 2. Flags - note that github.com/spf13/pflag is used in lieu of the standard flag package.
// 3. Environment variables.
// 4. Configuration file values.
// 5. Default values.
//
// NOTE: Viper key/value store and/or watching config sources is not yet supported.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package configurator
import (
"errors"
"fmt"
"go/ast"
"reflect"
"strconv"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const (
tagEnv = "env"
tagFlag = "flag"
tagFile = "file"
tagDefault = "default"
)
var (
// ErrValueNotStruct is returned when value passed to config.Load() is not a struct.
ErrValueNotStruct = errors.New("Value does not appear to be a struct!")
// ErrValueNotStructPointer is returned when value passed to config.Load() is not a pointer to a struct.
ErrValueNotStructPointer = errors.New("Value passed was not a struct pointer!")
)
var c *Config
func init() {
c = &Config{
FileName: "config",
FilePaths: []string{"."},
}
}
// Config is a convenience configuration struct built on top of Viper. You
// use Config by annotating your configuration struct with env, flag, file,
// and default tags which will be parsed by Config. You can either
// embed Configurator.Config in your struct or reference configurator.Load()
// directly. The priority of the sources is the same Viper:
// 1. overrides
// 2. flags
// 3. env. variables
// 4. config file
// 5. defaults
//
// For example, if you embedded configurator.Config in your struct and
// configured it like so:
//
// type AppConfig struct {
// configurator.Config
// Secret string `file:"secret" env:"APP_SECRET" flag:"secret" default:"abc123xyz"`
// User string `file:"user" env:"APP_USER" flag:"user" default:"root"`
// Environment string `file:"env" env:"APP_ENV" flag:"env" default:"dev"`
// }
//
// Assuming your source values were the following:
// File : {
// "user": "test_user"
// "secret": "defaultsecret"
// }
// Env : {
// "APP_SECRET": "somesecretkey"
// }
//
// This is how you would load the configuration:
//
// func loadConfig() {
// config := AppConfig{}
// err := config.Load(&config)
//
// if err != nil {
// // Always handle your errors
// log.Fatalf("Unable to load application configuration! Error: %s", err.Error())
// }
//
// fmt.Println("config.Secret =", config.Secret) // somesecretkey, from env
// fmt.Println("config.User =", config.User) // test_user, from file
// fmt.Println("config.Environment =", config.Environment) // dev, from defaults
// }
//
type Config struct {
// FileName is the name of the configuration file without any extensions.
FileName string
// FilePaths is an array of configuration file paths to search for the configuration file.
FilePaths []string
externalConfig *interface{}
accessors map[string][]string
viper *viper.Viper
}
// Load attempts to populate the struct with configuration values.
// The value passed to load must be a struct reference or an error
// will be returned.
func Load(structRef interface{}) error {
return c.Load(structRef)
}
// SetFileName specifies the name of the configuration file without any extensions.
func SetFileName(fileName string) {
c.FileName = fileName
}
// SetFilePaths specifies the array of configuration file paths to search for the configuration file.
func SetFilePaths(filePaths []string) {
c.FilePaths = filePaths
}
// Load attempts to populate the struct with configuration values.
// The value passed to load must be a struct reference or an error
// will be returned.
func (c *Config) Load(structRef interface{}) error {
c.viper = viper.New()
c.accessors = make(map[string][]string)
canLoadErr := c.canLoad(structRef)
if canLoadErr != nil {
return canLoadErr
}
ptrRef := reflect.ValueOf(structRef)
ref := ptrRef.Elem()
return c.parseStructConfigValues(ref, structRef)
}
func (c *Config) canLoad(structRef interface{}) error {
ptrRef := reflect.ValueOf(structRef)
if ptrRef.Kind() != reflect.Ptr {
return ErrValueNotStructPointer
}
elemRef := ptrRef.Elem()
if elemRef.Kind() != reflect.Struct {
return ErrValueNotStruct
}
return nil
}
/////////////
// Parsing //
/////////////
type parsedValue struct {
tagValue string
fieldType reflect.Type
}
func (c *Config) parseStructConfigValues(structRef reflect.Value, val interface{}) error {
// Parse configurator values on our struct
defaultValues := parseDefaultValues(structRef)
envValues := parseEnvValues(structRef)
flagValues := parseFlagValues(structRef)
configValues := parseConfigFileValues(structRef)
c.bindEnvValues(envValues)
c.bindFlagValues(flagValues)
c.bindConfigFileValues(configValues)
c.populateDefaults(defaultValues)
err := c.populateConfigStruct(structRef)
return err
}
func parseDefaultValues(structRef reflect.Value) map[string]parsedValue {
values := parseValuesForTag(structRef, tagDefault)
return values
}
func parseEnvValues(structRef reflect.Value) map[string]parsedValue {
values := parseValuesForTag(structRef, tagEnv)
return values
}
func parseFlagValues(structRef reflect.Value) map[string]parsedValue {
values := parseValuesForTag(structRef, tagFlag)
return values
}
func parseConfigFileValues(structRef reflect.Value) map[string]parsedValue {
values := parseValuesForTag(structRef, tagFile)
return values
}
func parseValuesForTag(structRef reflect.Value, tagName string) map[string]parsedValue {
values := map[string]parsedValue{}
structType := structRef.Type()
for i := 0; i < structType.NumField(); i++ {
structField := structType.Field(i)
tag := structField.Tag
tagValue := tag.Get(tagName)
if tagValue != "" && ast.IsExported(structField.Name) {
values[structField.Name] = parsedValue{tagValue, structField.Type}
}
}
return values
}
/////////////
// Binding //
/////////////
func (c *Config) bindEnvValues(envValues map[string]parsedValue) {
for k, v := range envValues {
c.bindAccessor(k, v.tagValue)
c.viper.BindEnv(k, v.tagValue)
}
}
func (c *Config) bindFlagValues(flagValues map[string]parsedValue) *pflag.FlagSet {
flagSet := pflag.NewFlagSet("configurator", pflag.PanicOnError)
for k, v := range flagValues {
c.bindAccessor(k, v.tagValue)
pflag.String(v.tagValue, "", "")
flag := pflag.Lookup(v.tagValue)
c.viper.BindPFlag(k, flag)
flagSet.AddFlag(flag)
}
return flagSet
}
func (c *Config) bindConfigFileValues(configValues map[string]parsedValue) {
c.viper.SetConfigName(c.FileName)
for _, filePath := range c.FilePaths {
c.viper.AddConfigPath(filePath)
}
// Map the config file keys to our variable
for k, v := range configValues {
c.bindAccessor(k, v.tagValue)
}
}
func (c *Config) bindAccessor(key string, value string) {
list, exists := c.accessors[key]
if !exists {
list = []string{value}
} else {
list = append(list, value)
}
c.accessors[key] = list
}
//////////////
// Populate //
//////////////
func (c *Config) populateDefaults(defaultValues map[string]parsedValue) {
for k, v := range defaultValues {
c.viper.SetDefault(k, v.tagValue)
c.bindAccessor(k, k)
}
}
func (c *Config) populateConfigStruct(structRef reflect.Value) error {
c.viper.ReadInConfig()
structType := structRef.Type()
for i := 0; i < structType.NumField(); i++ {
structField := structType.Field(i)
accessors := c.accessors[structField.Name]
for _, accessor := range accessors {
configValue := c.viper.Get(accessor)
if configValue != nil {
err := populateStructField(structField, structRef.Field(i), fmt.Sprintf("%v", configValue))
if err != nil {
return err
}
break
}
}
}
return nil
}
func populateStructField(field reflect.StructField, fieldValue reflect.Value, value string) error {
switch fieldValue.Kind() {
case reflect.String:
if isZeroOfUnderlyingType(fieldValue.Interface()) {
fieldValue.SetString(value)
}
case reflect.Bool:
bvalue, err := strconv.ParseBool(value)
if err != nil {
return fmt.Errorf("Unable to convert value (%s) for to bool for field: %s! Error: %s", value, field.Name, err.Error())
}
if isZeroOfUnderlyingType(fieldValue.Interface()) {
fieldValue.SetBool(bvalue)
}
case reflect.Float32, reflect.Float64:
floatValue, err := strconv.ParseFloat(value, 64)
if err != nil {
return fmt.Errorf("Unable to convert value (%s) for to float for field: %s! Error: %s", value, field.Name, err.Error())
}
if isZeroOfUnderlyingType(fieldValue.Interface()) {
fieldValue.SetFloat(floatValue)
}
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64:
intValue, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return fmt.Errorf("Unable to convert value (%s) for to int for field: %s! Error: %s", value, field.Name, err.Error())
}
if isZeroOfUnderlyingType(fieldValue.Interface()) {
fieldValue.SetInt(intValue)
}
case reflect.Uint, reflect.Uint8, reflect.Uint32, reflect.Uint64:
intValue, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return fmt.Errorf("Unable to convert value (%s) for to unsigned int for field: %s! Error: %s", value, field.Name, err.Error())
}
if isZeroOfUnderlyingType(fieldValue.Interface()) {
fieldValue.SetUint(intValue)
}
}
return nil
}
/////////////
// Utility //
/////////////
func isZeroOfUnderlyingType(x interface{}) bool {
// Source: http://stackoverflow.com/questions/13901819/quick-way-to-detect-empty-values-via-reflection-in-go
return x == reflect.Zero(reflect.TypeOf(x)).Interface()
}