forked from insolar/insconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigurator.go
553 lines (486 loc) · 14.4 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
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
package insconfig
import (
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"regexp"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
const placeholder = "<-key->"
// Params for config parsing
type Params struct {
// EnvPrefix is a prefix for environment variables
EnvPrefix string
// ViperHooks is custom viper decoding hooks
ViperHooks []mapstructure.DecodeHookFunc
// ConfigPathGetter should return config path
ConfigPathGetter ConfigPathGetter
// FileNotRequired - do not return error on file not found
FileNotRequired bool
}
// ConfigPathGetter - implement this if you don't want to use config path from --config flag
type ConfigPathGetter interface {
GetConfigPath() string
}
type insConfigurator struct {
params Params
viper *viper.Viper
configPath string
}
// New creates new insConfigurator with params
func New(params Params) insConfigurator {
return insConfigurator{
params: params,
configPath: params.ConfigPathGetter.GetConfigPath(),
viper: viper.New(),
}
}
// Load loads configuration from path, env and makes checks
// configStruct is a pointer to your config
func (i *insConfigurator) Load(configStruct interface{}) error {
if i.params.EnvPrefix == "" {
return errors.New("EnvPrefix should be defined")
}
if i.params.ConfigPathGetter == nil {
return errors.New("ConfigPathGetter should be defined")
}
return i.load(i.configPath, configStruct)
}
func (i *insConfigurator) load(path string, configStruct interface{}) error {
i.viper.AutomaticEnv()
i.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
i.viper.SetEnvPrefix(i.params.EnvPrefix)
i.viper.SetConfigFile(path)
if err := i.viper.ReadInConfig(); err != nil {
if !i.params.FileNotRequired {
return err
}
fmt.Printf("failed to load config from '%s'\n", path)
}
// this 'if' block necessary for check duplicated map keys in YAML
if !i.params.FileNotRequired {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return errors.Wrapf(err, "failed to read config file")
}
err = yaml.UnmarshalStrict(bytes, configStruct)
if err != nil && strings.Contains(err.Error(), "already set in map") {
return errors.Wrapf(err, "failed to unmarshal config file into configuration structure")
}
}
i.params.ViperHooks = append(i.params.ViperHooks, mapstructure.StringToTimeDurationHookFunc(), mapstructure.StringToSliceHookFunc(","))
err := i.viper.UnmarshalExact(configStruct, viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(
i.params.ViperHooks...,
)))
if err != nil {
return errors.Wrapf(err, "failed to unmarshal config file into configuration structure")
}
configStructKeys, err := deepFieldNames(configStruct, "", false)
if err != nil {
return err
}
configStructKeys, mapKeys := separateKeys(configStructKeys)
configStructKeys, err = i.checkNoExtraENVValues(configStructKeys, mapKeys)
if err != nil {
return err
}
for k := range mapKeys {
if used := mapKeys[k]; !used {
configStructKeys = append(configStructKeys, k)
}
}
err = i.checkAllValuesIsSet(configStructKeys)
if err != nil {
return err
}
// Second Unmarshal needed because of bug https://github.com/spf13/viper/issues/761
// This should be evaluated after manual values overriding is done
err = i.viper.UnmarshalExact(configStruct, viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(
i.params.ViperHooks...,
)))
if err != nil {
return errors.Wrapf(err, "failed to unmarshal config file into configuration structure 2")
}
return nil
}
func (i *insConfigurator) checkNoExtraENVValues(structKeys []string, mapKeys map[string]bool) ([]string, error) {
var errorKeys []string
prefixLen := len(i.params.EnvPrefix)
for _, e := range os.Environ() {
if len(e) > prefixLen && e[0:prefixLen]+"_" == strings.ToUpper(i.params.EnvPrefix)+"_" {
kv := strings.SplitN(e, "=", 2)
key := strings.ReplaceAll(strings.Replace(strings.ToLower(kv[0]), i.params.EnvPrefix+"_", "", 1), "_", ".")
if k, pref, match := matchMapKey(mapKeys, key); match && !stringInSlice(key, structKeys) {
structKeys = append(structKeys, newKeys(mapKeys, k, pref)...)
}
if stringInSlice(key, structKeys) {
// This manually sets value from ENV and overrides everything, this temporarily fix issue https://github.com/spf13/viper/issues/761
i.viper.Set(key, kv[1])
} else {
errorKeys = append(errorKeys, key)
}
}
}
if len(errorKeys) > 0 {
return structKeys, errors.New(fmt.Sprintf("Wrong config keys found in ENV: %s", strings.Join(errorKeys, ", ")))
}
return structKeys, nil
}
func separateKeys(list []string) ([]string, map[string]bool) {
var structKeys []string
mapKeys := make(map[string]bool)
for _, s := range list {
if strings.Contains(s, placeholder) {
mapKeys[s] = false
} else {
structKeys = append(structKeys, s)
}
}
return structKeys, mapKeys
}
func newKeys(keys map[string]bool, key, pref string) []string {
var names []string
oldStr := strings.Join([]string{pref, placeholder}, "")
newStr := strings.Join([]string{pref, key}, "")
for k := range keys {
if strings.HasPrefix(k, oldStr) {
names = append(names, strings.Replace(k, oldStr, newStr, 1))
keys[k] = true
}
}
return names
}
func matchMapKey(keys map[string]bool, key string) (string, string, bool) {
for k := range keys {
l := strings.ToLower(k)
pattern := strings.ReplaceAll(l, ".", "\\.")
pattern = strings.ReplaceAll(pattern, placeholder, ".+")
match, err := regexp.MatchString(pattern, key)
if err != nil {
fmt.Println(err)
}
if match {
parts := strings.Split(l, placeholder)
return strings.TrimSuffix(strings.TrimPrefix(key, parts[0]), parts[1]), parts[0], true
}
}
return "", "", false
}
func (i *insConfigurator) checkAllValuesIsSet(structKeys []string) error {
var errorKeys []string
allKeys := i.viper.AllKeys()
for _, keyName := range structKeys {
if !i.viper.IsSet(keyName) {
// Due to a bug https://github.com/spf13/viper/issues/447 we can't use InConfig, so
if !stringInSlice(keyName, allKeys) {
errorKeys = append(errorKeys, keyName)
}
// Value of this key is "null" but it's set in config file
}
}
if len(errorKeys) > 0 {
return errors.New(fmt.Sprintf("Keys is not defined in config: %s", strings.Join(errorKeys, ", ")))
}
return nil
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if strings.ToLower(b) == strings.ToLower(a) {
return true
}
}
return false
}
func deepFieldNames(iface interface{}, prefix string, inMap bool) ([]string, error) {
names := make([]string, 0)
ifv := reflect.Indirect(reflect.ValueOf(iface))
switch ifv.Kind() {
case reflect.Struct:
for i := 0; i < ifv.Type().NumField(); i++ {
v := ifv.Field(i)
tagValue := ifv.Type().Field(i).Tag.Get("mapstructure")
tagParts := strings.Split(tagValue, ",")
// If "squash" is specified in the tag, we squash the field down.
squash := false
for _, tag := range tagParts[1:] {
if tag == "squash" {
squash = true
break
}
}
newPrefix := ""
currPrefix := ""
if !squash {
currPrefix = ifv.Type().Field(i).Name
}
if prefix != "" {
newPrefix = strings.Join([]string{prefix, currPrefix}, ".")
} else {
newPrefix = currPrefix
}
fieldNames, err := deepFieldNames(v.Interface(), strings.ToLower(newPrefix), inMap)
if err != nil {
return nil, err
}
names = append(names, fieldNames...)
}
case reflect.Map:
if inMap {
return nil, errors.New("nested maps are not allowed in config")
}
inMap = true
keyKind := ifv.Type().Key().Kind()
if keyKind != reflect.String {
return nil, errors.New(fmt.Sprintf("maps in config must have string keys but got: %s key in %s", keyKind, ifv.Type()))
}
if len(ifv.MapKeys()) != 0 {
for _, k := range ifv.MapKeys() {
key := k.String()
newPrefix := ""
if prefix != "" {
newPrefix = strings.Join([]string{prefix, key}, ".")
} else {
newPrefix = key
}
fieldNames, err := deepFieldNames(ifv.MapIndex(k).Interface(), strings.ToLower(newPrefix), inMap)
if err != nil {
return nil, err
}
names = append(names, fieldNames...)
}
} else {
newPrefix := ""
if prefix != "" {
newPrefix = strings.Join([]string{prefix, placeholder}, ".")
} else {
newPrefix = placeholder
}
e := ifv.Type().Elem()
value := reflect.Zero(e)
fieldNames, err := deepFieldNames(value.Interface(), strings.ToLower(newPrefix), inMap)
if err != nil {
return nil, err
}
names = append(names, fieldNames...)
}
inMap = false
default:
if prefix != "" {
names = append(names, strings.ToLower(prefix))
}
}
return names, nil
}
// ToYaml returns yaml marshalled struct
func (i *insConfigurator) ToYaml(c interface{}) string {
// todo clean password
out, err := yaml.Marshal(c)
if err != nil {
return fmt.Sprintf("failed to marshal config structure: %v", err)
}
return string(out)
}
// Empty config generation part
// This function is depricated, because it can not work with default slices and maps. Use YamlTemplatableStruct instead.
// you may use insconfig:"default|comment" Tag on struct fields to express your feelings.
// Also you can use insconfigsecret tag for hide field from dumping its value
// Deprecated. Use YamlTemplatableStruct instead.
type YamlTemplatable interface {
TemplateTo(w io.Writer, m *YamlTemplater) error
}
// Deprecated. Use YamlTemplaterStruct instead.
type YamlTemplater struct {
Obj interface{} // what are we marshaling right now
Level int // Level of recursion
Tag reflect.StructTag // Tag for current field
FName string // current field name
}
// Deprecated. Use NewYamlTemplaterStruct instead.
func NewYamlTemplater(obj interface{}) *YamlTemplater {
return &YamlTemplater{
Obj: obj,
Level: -1,
Tag: "",
}
}
func (m *YamlTemplater) TemplateTo(w io.Writer) error {
if o, ok := m.Obj.(YamlTemplatable); ok {
return o.TemplateTo(w, m)
}
// HINT m need the same to work on (z *Z)TemplateTo()
t := reflect.TypeOf(m.Obj)
v := reflect.Zero(t)
if t.Kind() == reflect.Ptr {
m.Obj = reflect.Zero(t.Elem()).Interface()
return m.TemplateTo(w)
}
indent := ""
if m.Level > 0 {
indent = strings.Repeat(" ", m.Level)
}
d := ""
c := ""
yfname := ""
if cont, ok := m.Tag.Lookup("insconfig"); ok { // detect tags
arr := strings.SplitN(cont, "|", 2)
if len(arr) == 2 {
d = arr[0]
c = arr[1]
} else {
c = arr[0]
}
}
if cont, ok := m.Tag.Lookup("yaml"); ok {
yfname = cont
}
if c != "" { // write down a comment
if _, err := fmt.Fprintf(w, "%s#%s\n", indent, c); err != nil {
return err
}
}
if m.FName != "" && t.Kind() != reflect.Array {
if yfname == "" {
yfname = strings.ToLower(m.FName)
}
if _, err := fmt.Fprintf(w, "%s%s: ", indent, yfname); err != nil {
return err
}
}
switch t.Kind() { // main switch
case reflect.Struct: // no default
if _, err := fmt.Fprint(w, "\n"); err != nil {
return err
}
for i := 0; i < t.NumField(); i++ {
if err := (&YamlTemplater{
Obj: v.Field(i).Interface(),
Level: m.Level + 1,
Tag: t.Field(i).Tag,
FName: t.Field(i).Name,
}).TemplateTo(w); err != nil {
return errors.Wrapf(err, "in field %s", t.Field(i).Name)
}
}
case reflect.Map:
if _, err := fmt.Fprintf(w, "# <map> of %s \n%s somekey: ", t.Elem(), indent); err != nil {
return err
}
if err := (&YamlTemplater{
Obj: reflect.Zero(t.Elem()).Interface(),
Level: m.Level + 1,
}).TemplateTo(w); err != nil {
return err
}
case reflect.Array, reflect.Slice:
if _, err := fmt.Fprintf(w, "# <array> of %s \n%s - ", t.Elem(), indent); err != nil {
return err
}
if err := (&YamlTemplater{
Obj: reflect.Zero(t.Elem()).Interface(),
Level: m.Level + 1,
}).TemplateTo(w); err != nil {
return err
}
case reflect.String, // all scalars
reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64,
reflect.Complex64, reflect.Complex128:
_, err := fmt.Fprintf(w, "%s # %s\n", d, t.Name())
return err
default:
return fmt.Errorf("unknown serialization for type %s kind %s (please implement YamlTemplatable)", t.Name(), t.Kind())
}
return nil
}
type YamlDumpable interface {
DumpTo(w io.Writer, m *YamlDumper) error
}
type YamlDumper struct {
Obj interface{} // what are we marshaling right now
Level int // Level of recursion
Tag reflect.StructTag // Tag for current field
FName string // current field name
}
func NewYamlDumper(obj interface{}) *YamlDumper {
return &YamlDumper{
Obj: obj,
Level: 0,
Tag: "",
}
}
func (d *YamlDumper) DumpTo(w io.Writer) error {
if o, ok := d.Obj.(YamlDumpable); ok {
return o.DumpTo(w, d)
}
t := reflect.TypeOf(d.Obj)
v := reflect.ValueOf(d.Obj)
if t.Kind() == reflect.Ptr {
d.Obj = v.Elem().Interface()
return d.DumpTo(w)
}
if _, ok := d.Tag.Lookup("insconfigsecret"); ok { // detect tags
v = reflect.ValueOf(`"*****"`)
}
indent := strings.Repeat(" ", d.Level)
switch t.Kind() { // main switch
case reflect.Struct: // no default
fmt.Fprint(w, "\n")
for i := 0; i < t.NumField(); i++ {
v := v.Field(i)
t := t.Field(i)
yfname := ""
if cont, ok := t.Tag.Lookup("yaml"); ok {
yfname = cont
} else {
yfname = strings.ToLower(t.Name)
}
fmt.Fprintf(w, "%s%s: ", indent, yfname)
if err := (&YamlDumper{
Obj: v.Interface(),
Level: d.Level + 1,
Tag: t.Tag,
FName: t.Name,
}).DumpTo(w); err != nil {
return errors.Wrapf(err, "in field %s", t.Name)
}
}
case reflect.Map:
fmt.Fprint(w, "\n")
i := v.MapRange()
for i.Next() {
fmt.Fprintf(w, "%s%s: ", indent, i.Key().Interface())
if err := (&YamlDumper{
Obj: i.Value().Interface(),
Level: d.Level + 1,
}).DumpTo(w); err != nil {
return err
}
}
case reflect.Array, reflect.Slice:
fmt.Fprint(w, "\n")
for i := 0; i < v.Len(); i++ {
fmt.Fprintf(w, "%s - ", indent)
if err := (&YamlDumper{
Obj: v.Index(i).Interface(),
Level: d.Level + 1,
}).DumpTo(w); err != nil {
return err
}
}
case reflect.String, // all scalars
reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64,
reflect.Complex64, reflect.Complex128:
_, err := fmt.Fprintf(w, "%v\n", v.Interface())
return err
}
return nil
}