-
Notifications
You must be signed in to change notification settings - Fork 1
/
confucius.go
555 lines (490 loc) · 14.3 KB
/
confucius.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package confucius
import (
"embed"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/imdario/mergo"
"github.com/mitchellh/mapstructure"
"github.com/pelletier/go-toml"
"gopkg.in/yaml.v2"
)
const (
// DefaultFilename is the default filename of the config file that confucius looks for.
DefaultFilename = "config.yaml"
// DefaultDir is the default directory that confucius searches in for the config file.
DefaultDir = "."
// DefaultTag is the default struct tag key that confucius uses to find the field's alt
// name.
DefaultTag = "conf"
// DefaultTimeLayout is the default time layout that confucius uses to parse times.
DefaultTimeLayout = time.RFC3339
// DefaultProfileLayout represents default profile file layout.
// You should use `config` for filename, `test` for profile, `yaml` for extension.
// Example; config-test.yaml
DefaultProfileLayout = "config.test.yaml"
// MainFileIndicator is config file type indicator
MainFileIndicator = "#main"
// MainFileIndicator is config file type indicator
ProfileFileIndicator = "#profile"
// FileEmbedLocationIndicator is config file location indicator
EmbedLocationIndicator = "#embed"
// FileEmbedLocationIndicator is config file location indicator
LocalLocationIndicator = "#local"
)
type decodedObject map[string]interface{}
func defaultConfucius() *confucius {
return &confucius{
filename: DefaultFilename,
dirs: []string{DefaultDir},
tag: DefaultTag,
timeLayout: DefaultTimeLayout,
profileLayout: DefaultProfileLayout,
logger: defaultLogger(),
}
}
type confucius struct {
useEnv bool
useReader bool
useEmbedFS bool
dirs []string
profiles []string
expectedConfigFiles []string
filename string
tag string
timeLayout string
envPrefix string
profileLayout string
readerConfig io.Reader
readerDecoder Decoder
embedFS embed.FS
logger *logger
}
// Load reads a configuration file and loads it into the given struct. The
// parameter `cfg` must be a pointer to a struct.
//
// By default confucius looks for a file `config.yaml` in the current directory and
// uses the struct field tag `fig` for matching field names and validation.
// To alter this behaviour pass additional parameters as options.
//
// A field can be marked as required by adding a `required` key in the field's struct tag.
// If a required field is not set by the configuration file an error is returned.
//
// type Config struct {
// Env string `conf:"env" validate:"required"` // or just `validate:"required"`
// }
//
// A field can be configured with a default value by adding a `default` key in the
// field's struct tag.
// If a field is not set by the configuration file then the default value is set.
//
// type Config struct {
// Level string `conf:"level" default:"info"` // or just `default:"info"`
// }
//
// A single field may not be marked as both `required` and `default`.
func Load(cfg interface{}, options ...Option) error {
c := defaultConfucius()
for _, opt := range options {
opt(c)
}
return c.Load(cfg)
}
func (c *confucius) Load(cfg interface{}) (err error) {
c.logger.Debug("confucius starting")
if !isStructPtr(cfg) {
return fmt.Errorf("cfg must be a pointer to a struct")
}
vals := make(decodedObject)
if c.useReader {
vals, err = c.decodeReader(c.readerConfig, c.readerDecoder)
if err != nil {
return err
}
}
files, err := c.findFiles()
if err != nil && !(c.useReader || c.useEnv) {
return err
}
if vals, err = c.decodeFiles(files, vals); err != nil {
return err
}
if err := c.decodeMap(vals, cfg); err != nil {
return err
}
return c.processCfg(cfg)
}
func (c *confucius) findFiles() ([]string, error) {
c.initExpectedConfigFiles()
result := []string{}
files, err := c.findEmbedFiles()
if err != nil {
return result, err
}
result = append(result, files...)
result = append(result, c.findLocalFiles()...)
if len(c.expectedConfigFiles) > 0 {
return nil, fmt.Errorf("\"%s\" file(s) not found: %w",
strings.Join(c.expectedConfigFiles, "\", \""),
ErrFileNotFound,
)
}
sort.StringSlice(result).Sort()
return result, nil
}
func (c *confucius) findLocalFiles() (acc []string) {
found := map[string]bool{}
for _, dir := range c.dirs {
path := filepath.Join(dir, c.filename)
if fileExists(path) && !found[c.filename] {
found[c.filename] = true
c.removeFromExpectedList(c.filename)
acc = append(acc,
fmt.Sprintf("%s:%s=%s", LocalLocationIndicator, MainFileIndicator, path),
)
}
for idx, profile := range c.profiles {
profileName := c.profileFileName(profile)
path := filepath.Join(dir, profileName)
if fileExists(path) && !found[profileName] {
found[profileName] = true
c.removeFromExpectedList(profileName)
acc = append(acc,
fmt.Sprintf("%s:%s_%02d_%s=%s", LocalLocationIndicator, ProfileFileIndicator, idx, profile, path),
)
}
}
}
return
}
func (c *confucius) findEmbedFiles() (acc []string, err error) {
found := map[string]bool{}
if c.useEmbedFS {
err = c.walkEmbedDir(&acc, found, ".")
if err != nil {
return
}
}
return
}
func (c confucius) fileExists(filename string) string {
if c.filename == filename {
return MainFileIndicator
}
for idx, profile := range c.profiles {
profileName := c.profileFileName(profile)
if profileName == filename {
return fmt.Sprintf("%s_%02d_%s", ProfileFileIndicator, idx, profile)
}
}
return ""
}
func (c *confucius) walkEmbedDir(accumulator *[]string, found map[string]bool, path string) error {
entries, err := c.embedFS.ReadDir(path)
if err != nil {
return err
}
sort.Slice(entries, func(i, j int) bool {
return !entries[i].IsDir()
})
for _, entry := range entries {
fullPath := filepath.Join(path, entry.Name())
if entry.IsDir() {
if err := c.walkEmbedDir(accumulator, found, fullPath); err != nil {
return err
}
} else if tag := c.fileExists(entry.Name()); tag != "" && !found[entry.Name()] {
found[entry.Name()] = true
c.removeFromExpectedList(entry.Name())
*accumulator = append(*accumulator, fmt.Sprintf("%s:%s=%s", EmbedLocationIndicator, tag, fullPath))
} else {
c.logger.Debug("file not found: %+v", fullPath)
}
}
return nil
}
func (c *confucius) initExpectedConfigFiles() {
c.expectedConfigFiles = []string{c.filename}
for _, profile := range c.profiles {
c.expectedConfigFiles = append(c.expectedConfigFiles, c.profileFileName(profile))
}
}
func (c *confucius) removeFromExpectedList(file string) {
result := []string{}
for _, expected := range c.expectedConfigFiles {
if expected == file {
continue
}
result = append(result, expected)
}
c.expectedConfigFiles = result
}
func (c *confucius) decodeEmbedFile(file string) (vals decodedObject, err error) {
fd, err := c.embedFS.Open(file)
if err != nil {
return nil, err
}
defer fd.Close()
return c.decodeReader(fd, Decoder(filepath.Ext(file)))
}
func (c *confucius) decodeFiles(files []string, origin decodedObject) (vals decodedObject, err error) {
vals = origin
for _, file := range files {
fileVals := decodedObject{}
sections := strings.Split(file, "=")
if strings.Contains(file, EmbedLocationIndicator) {
fileVals, err = c.decodeEmbedFile(sections[1])
if err != nil {
return nil, err
}
}
if strings.Contains(file, LocalLocationIndicator) {
fileVals, err = c.decodeFile(sections[1])
if err != nil {
return nil, err
}
}
if err := mergo.Merge(&vals, fileVals, mergo.WithOverride, mergo.WithTypeCheck); err != nil {
return nil, err
}
}
return vals, nil
}
func (c *confucius) profileFileName(profile string) string {
filename := c.profileLayout
parts := strings.Split(c.filename, ".")
filename = strings.ReplaceAll(filename, "config", parts[0])
filename = strings.ReplaceAll(filename, "test", profile)
filename = strings.ReplaceAll(filename, "yaml", parts[1])
return filename
}
// decodeFile reads the file and unmarshalls // it using a decoder based on the file extension.
func (c *confucius) decodeFile(file string) (decodedObject, error) {
fd, err := os.Open(file)
if err != nil {
return nil, err
}
defer fd.Close()
return c.decodeReader(fd, Decoder(filepath.Ext(file)))
}
func (c *confucius) decodeReader(reader io.Reader, decoder Decoder) (decodedObject, error) {
vals := make(decodedObject)
switch decoder {
case ".yaml", ".yml":
if err := yaml.NewDecoder(reader).Decode(&vals); err != nil {
return nil, err
}
case ".json":
if err := json.NewDecoder(reader).Decode(&vals); err != nil {
return nil, err
}
case ".toml":
tree, err := toml.LoadReader(reader)
if err != nil {
return nil, err
}
for field, val := range tree.ToMap() {
vals[field] = val
}
default:
return nil, fmt.Errorf("unsupported file extension %s", filepath.Ext(c.filename))
}
return vals, nil
}
// decodeMap decodes a map of va// lues into result using the mapstructure library.
func (c *confucius) decodeMap(m decodedObject, result interface{}) error {
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
WeaklyTypedInput: true,
Result: result,
TagName: c.tag,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
fromEnvironmentHookFunc(),
mapstructure.StringToTimeDurationHookFunc(),
mapstructure.StringToTimeHookFunc(c.timeLayout),
),
})
if err != nil {
return err
}
return dec.Decode(m)
}
func replaceEnvironments(str string) (result string, err error) {
re := regexp.MustCompile(`\$\{(.*?|)\}`)
result = str
for _, match := range re.FindAllStringSubmatch(str, -1) {
whole, value := match[0], match[1]
if value == "" {
return result, fmt.Errorf("environment name is missing")
}
s := strings.Split(value, ":")
envName := s[0]
if envValue, ok := os.LookupEnv(envName); ok {
result = strings.ReplaceAll(result, whole, envValue)
} else {
defaultVal := ""
if len(s) > 1 {
defaultVal = strings.ReplaceAll(value, envName+":", "")
}
result = strings.ReplaceAll(result, whole, defaultVal)
}
}
return result, err
}
func fromEnvironmentHookFunc() mapstructure.DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
return replaceEnvironments(data.(string))
}
}
// processCfg processes a cfg struct after it has been loaded from
// the config file, by validating required fields and setting defaults
// where applicable.
func (c *confucius) processCfg(cfg interface{}) error {
fields := flattenCfg(cfg, c.tag)
errs := make(fieldErrors)
for _, field := range fields {
if err := c.processField(field); err != nil {
errs[field.path()] = err
}
}
if len(errs) > 0 {
return errs
}
return nil
}
// processField processes a single field and is called by processCfg
// for each field in cfg.
func (c *confucius) processField(field *field) error {
if field.required && field.setDefault {
return fmt.Errorf("field cannot have both a required validation and a default value")
}
if c.useEnv {
if err := c.setFromEnv(field.v, field.path()); err != nil {
return fmt.Errorf("unable to set from env: %v", err)
}
}
if field.required && isZero(field.v) {
return fmt.Errorf("required validation failed")
}
if field.setDefault && isZero(field.v) {
if err := c.setDefaultValue(field.v, field.defaultVal); err != nil {
return fmt.Errorf("unable to set default: %v", err)
}
}
return nil
}
func (c *confucius) setFromEnv(fv reflect.Value, key string) error {
key = c.formatEnvKey(key)
if val, ok := os.LookupEnv(key); ok {
return c.setValue(fv, val)
}
return nil
}
func (c *confucius) formatEnvKey(key string) string {
// loggers[0].level --> loggers_0_level
key = strings.NewReplacer(".", "_", "[", "_", "]", "").Replace(key)
if c.envPrefix != "" {
key = fmt.Sprintf("%s_%s", c.envPrefix, key)
}
return strings.ToUpper(key)
}
// setDefaultValue calls setValue but disallows booleans from
// being set.
func (c *confucius) setDefaultValue(fv reflect.Value, val string) error {
if fv.Kind() == reflect.Bool {
return fmt.Errorf("unsupported type: %v", fv.Kind())
}
return c.setValue(fv, val)
}
// setValue sets fv to val. it attempts to convert val to the correct
// type based on the field's kind. if conversion fails an error is
// returned.
// fv must be settable else this panics.
func (c *confucius) setValue(fv reflect.Value, val string) error {
switch fv.Kind() {
case reflect.Ptr:
if fv.IsNil() {
fv.Set(reflect.New(fv.Type().Elem()))
}
return c.setValue(fv.Elem(), val)
case reflect.Slice:
if err := c.setSlice(fv, val); err != nil {
return err
}
case reflect.Bool:
b, err := strconv.ParseBool(val)
if err != nil {
return err
}
fv.SetBool(b)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if _, ok := fv.Interface().(time.Duration); ok {
d, err := time.ParseDuration(val)
if err != nil {
return err
}
fv.Set(reflect.ValueOf(d))
} else {
i, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return err
}
fv.SetInt(i)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
i, err := strconv.ParseUint(val, 10, 64)
if err != nil {
return err
}
fv.SetUint(i)
case reflect.Float32, reflect.Float64:
f, err := strconv.ParseFloat(val, 64)
if err != nil {
return err
}
fv.SetFloat(f)
case reflect.String:
fv.SetString(val)
case reflect.Struct: // struct is only allowed a default in the special case where it's a time.Time
if _, ok := fv.Interface().(time.Time); ok {
t, err := time.Parse(c.timeLayout, val)
if err != nil {
return err
}
fv.Set(reflect.ValueOf(t))
} else {
return fmt.Errorf("unsupported type %s", fv.Kind())
}
default:
return fmt.Errorf("unsupported type %s", fv.Kind())
}
return nil
}
// setSlice val to sv. val should be a Go slice formatted as a string
// (e.g. "[1,2]") and sv must be a slice value. if conversion of val
// to a slice fails then an error is returned.
// sv must be settable else this panics.
func (c *confucius) setSlice(sv reflect.Value, val string) error {
ss := stringSlice(val)
slice := reflect.MakeSlice(sv.Type(), len(ss), cap(ss))
for i, s := range ss {
if err := c.setValue(slice.Index(i), s); err != nil {
return err
}
}
sv.Set(slice)
return nil
}