-
Notifications
You must be signed in to change notification settings - Fork 300
/
config.go
614 lines (519 loc) · 18 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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package config wraps Viper. It also allows to set a struct with defaults and generates pflags
package config
import (
"crypto/tls"
"fmt"
"io"
"os"
"reflect"
"sort"
"strings"
"time"
"github.com/mitchellh/mapstructure"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/types"
)
// TimeFormat is the format to parse times in.
const TimeFormat = time.RFC3339Nano
// Manager is a manager for the configuration.
type Manager struct {
name string
envPrefix string
viper *viper.Viper
flags *pflag.FlagSet
replacer *strings.Replacer
defaults interface{}
defaultPaths []string
configFlag string
dataDirFlag string
}
// Flags to be used in the command.
func (m *Manager) Flags() *pflag.FlagSet {
return m.flags
}
// EnvKeyReplacer sets the strings.Replacer for mapping mapping an environment variables to a key that does
// not match them.
func EnvKeyReplacer(r *strings.Replacer) Option {
return func(m *Manager) {
m.viper.SetEnvKeyReplacer(r)
m.replacer = r
}
}
// AllEnvironment returns all environment variables.
func (m *Manager) AllEnvironment() []string {
keys := m.AllKeys()
env := make([]string, 0, len(keys))
for _, key := range keys {
env = append(env, m.EnvironmentForKey(key))
}
return env
}
// AllKeys returns all keys holding a value, regardless of where they are set.
// Nested keys are returned with a "." separator.
func (m *Manager) AllKeys() []string {
keys := m.viper.AllKeys()
sort.Strings(keys)
return keys
}
// EnvironmentForKey returns the name of the environment variable for the given config key.
func (m *Manager) EnvironmentForKey(key string) string {
return strings.ToUpper(m.replacer.Replace(m.envPrefix + "." + key))
}
// Get returns the current value of the given config key.
func (m *Manager) Get(key string) interface{} {
return m.viper.Get(key)
}
// Option is the type of an option for the manager.
type Option func(m *Manager)
func WithDeprecatedFlag(name, usageMessage string) Option {
return func(m *Manager) {
m.flags.MarkDeprecated(name, usageMessage)
}
}
// DefaultOptions are the default options.
var DefaultOptions = []Option{
EnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")),
WithConfigFileFlag("config"),
}
// Initialize a new config manager with the given name and defaults.
// defaults should be a struct wiath fields that define the possible config flags by setting the struct tags.
// Possible struct tags are:
//
// `name:"<name>"` Defines the name of the config flag, in the environment, on the command line and in the config files.
// `shorthand:"<n>"` Defines a shorthand name for use on the command line.
// `description:"<description>"` Add a description that will be printed in the command's help message.
// `file-only:"<true|false>"` Denotes wether or not to attempt to parse this variable from the command line and environment or only from the
// config file. This can be used to allow complicated types to exist in the config file but not on the command line.
//
// The type of the struct fields also defines their type when parsing the config file, command line arguments or environment
// variables. Currently, the following types are supported:
//
// bool
// int, int8, int16, int32, int64
// uint, uint8, uint16, uint32, uint64
// float32, float64
// string
// time.Time Parsed according to the TimeFormat variable set in this package
// time.Duration Parsed by time.ParseDuration
// []string Parsed by splitting on whitespace or by passing multiple flags
// VAR="a b c" or --var a --var b --var c
// map[string]string Parsed by key=val pairs
// VAR="k=v q=r" or --var k=v --var q=r
// map[string][]byte Parsed by key=val pairs, val must be hex
// VAR="k=0x01 q=0x02" or --var k=0x01 --var q=0x02
// map[string][]string Parsed by key=val pairs where keys are repeated
// VAR="k=v1 k=v2 q=r" or --var k=v1 --var k=v2 --var q=r
// Configurable Parsed by the UnmarshalConfigString method
// structs with fields of these types The nested config names will be prefixed by the name of this struct, unless it is `name:",squash"`
// in which case the names are merged into the parent struct.
func Initialize(name, envPrefix string, defaults interface{}, opts ...Option) *Manager {
m := &Manager{
name: name,
envPrefix: envPrefix,
viper: viper.New(),
flags: pflag.NewFlagSet(name, pflag.ExitOnError),
replacer: strings.NewReplacer(),
defaults: defaults,
}
m.viper.SetTypeByDefaultValue(true)
m.viper.SetConfigName(name)
m.viper.SetConfigType("yml")
m.viper.AllowEmptyEnv(true)
m.viper.SetEnvPrefix(envPrefix)
m.viper.AutomaticEnv()
m.viper.AddConfigPath(".")
m.flags.SetInterspersed(true)
if defaults != nil {
m.setDefaults("", m.flags, defaults)
}
for _, opt := range opts {
opt(m)
}
err := m.viper.BindPFlags(m.flags)
if err != nil {
panic(err)
}
return m
}
// WithConfig returns a new flagset with has the flags of the Manager as well as the additional flags defined
// from the defaults passed along.
// Use this to build derived flagsets with a shared base config (for instance with cobra).
func (m *Manager) WithConfig(defaults interface{}) *pflag.FlagSet {
flags := pflag.NewFlagSet(m.name, pflag.ExitOnError)
flags.AddFlagSet(m.flags)
if defaults != nil {
m.setDefaults("", flags, defaults)
}
err := m.viper.BindPFlags(flags)
if err != nil {
panic(err)
}
return flags
}
// InitializeWithDefaults is the same as Initialize but it sets some sane default options (see DefaultOptions)
// alongside the passed in options.
func InitializeWithDefaults(name, envPrefix string, defaults interface{}, opts ...Option) *Manager {
return Initialize(name, envPrefix, defaults, append(DefaultOptions, opts...)...)
}
// Parse parses the command line arguments.
func (m *Manager) Parse(flags ...string) error {
return m.flags.Parse(flags)
}
// Unmarshal unmarshals the available config keys into the result. It matches the names of fields based on the name struct tag.
func (m *Manager) Unmarshal(result interface{}) error {
d, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "name",
ZeroFields: true,
WeaklyTypedInput: true,
Result: result,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
stringToTimeHookFunc(TimeFormat),
stringSliceToStringMapHookFunc,
stringSliceToStringMapStringSliceHookFunc,
stringToStringMapHookFunc,
stringToBufferMapHookFunc,
stringSliceToStringHookFunc,
configurableInterfaceHook,
configurableInterfaceSliceHook,
stringToByteSliceHook,
stringToByteArrayHook,
stringToTimeDurationPointerHook,
stringToADRAckDelayExponentPointerHook,
stringToADRAckLimitExponentPointerHook,
stringToAggregatedDutyCyclePointerHook,
stringToRxDelayPointerHook,
),
})
if err != nil {
return err
}
return d.Decode(m.viper.AllSettings())
}
// the path must be in default paths
func (m *Manager) isDefault(path string) bool {
for _, def := range m.defaultPaths {
if def == path {
return true
}
}
return false
}
func (m *Manager) inCLIFlags(path string) bool {
flags, err := m.flags.GetStringSlice(m.configFlag)
if err != nil {
return false
}
for _, flag := range flags {
if path == flag {
return true
}
}
return false
}
// ReadInConfig will read in all defined config files (according to the config file flag set by WithConfigFileFlag).
// The parsed config files will be merged into the config struct.
func (m *Manager) ReadInConfig() error {
files := m.viper.GetStringSlice(m.configFlag)
// read config files in reverse order, so config files listed first take precedence
for index := len(files) - 1; index >= 0; index-- {
file := files[index]
// ignore default config files that are missing or do not have read permissions
if m.isDefault(file) && !m.inCLIFlags(file) {
if _, err := os.Stat(file); os.IsNotExist(err) || os.IsPermission(err) {
continue
}
}
m.viper.SetConfigFile(file)
err := m.viper.MergeInConfig()
if err != nil {
return err
}
}
return nil
}
// mergeConfig merges the config from the reader as a yml config file.
func (m *Manager) mergeConfig(in io.Reader) error {
return m.viper.MergeConfig(in)
}
// UnmarshalKey unmarshals a specific key into a destination, which must have a matching type.
// This is useful for fields which have the `file-only:"true"` tag set and so are ignored when
// Unmarshalling them to a struct.
func (m *Manager) UnmarshalKey(key string, raw interface{}) error {
return m.viper.UnmarshalKey(key, raw)
}
// Configurable is the interface for things that can be configured.
// Implement the interface to add custom parsing to config variables from strings.
// For instance, to parse a log level from the strings "fatal", "error", etc into a custom
// enum for log levels.
type Configurable interface {
// UnmarshalConfigString parses a string into the config variable
UnmarshalConfigString(string) error
}
// Stringer is the interface for config variables that have a custom string representation.
// Implement next to Configurable if you want custom parsing and formatting for a type, and if the formatting
// needs to be different from fmt.String for some reason.
type Stringer interface {
// ConfigString returns the config string representation of type
ConfigString() string
}
var configurableI = reflect.TypeOf((*Configurable)(nil)).Elem()
func isConfigurableType(t reflect.Type) bool {
return t.Implements(configurableI) || reflect.PtrTo(t).Implements(configurableI)
}
func (m *Manager) setDefaults(prefix string, flags *pflag.FlagSet, config interface{}) {
configValue := reflect.ValueOf(config)
configKind := configValue.Type().Kind()
if configKind == reflect.Interface || configKind == reflect.Ptr {
configValue = configValue.Elem()
configKind = configValue.Type().Kind()
}
if configKind != reflect.Struct {
panic("default config is not a struct type")
}
for i := 0; i < configValue.NumField(); i++ {
field := configValue.Type().Field(i)
name := field.Tag.Get("name")
if name == "-" {
continue
}
if name == "" {
name = strings.ToLower(field.Name)
}
if prefix != "" {
name = prefix + "." + name
}
// skip previously defined flags
if f := flags.Lookup(name); f != nil {
continue
}
description := field.Tag.Get("description")
shorthand := field.Tag.Get("shorthand")
fileOnly := field.Tag.Get("file-only")
if configValue.Field(i).CanInterface() {
fieldKind := field.Type.Kind()
fieldValue := configValue.Field(i)
face := fieldValue.Interface()
// if it's only for in the file, skip the rest
if fileOnly == "true" {
m.viper.SetDefault(name, face)
continue
}
if isConfigurableType(field.Type) {
val := fmt.Sprintf("%v", face)
if str, ok := face.(fmt.Stringer); ok {
val = str.String()
}
if cstr, ok := face.(Stringer); ok {
val = cstr.ConfigString()
}
m.viper.SetDefault(name, val)
m.flags.StringP(name, shorthand, val, description)
continue
}
if fieldKind == reflect.Slice && isConfigurableType(field.Type.Elem()) {
val := configValue.Field(i)
n := val.Len()
defs := make([]string, 0, n)
for j := 0; j < n; j++ {
c := val.Index(j).Interface()
str := fmt.Sprintf("%v", c)
if s, ok := c.(fmt.Stringer); ok {
str = s.String()
}
if s, ok := c.(Stringer); ok {
str = s.ConfigString()
}
defs = append(defs, str)
}
m.viper.SetDefault(name, defs)
m.flags.StringSliceP(name, shorthand, defs, description)
continue
}
switch val := face.(type) {
case bool:
m.viper.SetDefault(name, val)
flags.BoolP(name, shorthand, val, description)
case int:
m.viper.SetDefault(name, val)
flags.IntP(name, shorthand, val, description)
case int8:
m.viper.SetDefault(name, val)
flags.IntP(name, shorthand, int(val), description)
case int16:
m.viper.SetDefault(name, val)
flags.IntP(name, shorthand, int(val), description)
case int32:
m.viper.SetDefault(name, val)
flags.IntP(name, shorthand, int(val), description)
case int64:
m.viper.SetDefault(name, val)
flags.IntP(name, shorthand, int(val), description)
case uint:
m.viper.SetDefault(name, val)
flags.UintP(name, shorthand, val, description)
case uint8:
m.viper.SetDefault(name, val)
flags.UintP(name, shorthand, uint(val), description)
case uint16:
m.viper.SetDefault(name, val)
flags.UintP(name, shorthand, uint(val), description)
case uint32:
m.viper.SetDefault(name, val)
flags.UintP(name, shorthand, uint(val), description)
case uint64:
m.viper.SetDefault(name, val)
flags.UintP(name, shorthand, uint(val), description)
case float32:
m.viper.SetDefault(name, val)
flags.Float64P(name, shorthand, float64(val), description)
case float64:
m.viper.SetDefault(name, val)
flags.Float64P(name, shorthand, val, description)
case *uint32:
var def string
if val != nil {
def = fmt.Sprintf("%v", *val)
m.viper.SetDefault(name, *val)
}
flags.StringP(name, shorthand, def, description)
case *float32:
var def string
if val != nil {
def = fmt.Sprintf("%v", *val)
m.viper.SetDefault(name, *val)
}
flags.StringP(name, shorthand, def, description)
case string:
m.viper.SetDefault(name, val)
flags.StringP(name, shorthand, val, description)
case *string:
var def string
if val != nil {
m.viper.SetDefault(name, *val)
}
flags.StringP(name, shorthand, def, description)
case time.Time:
m.viper.SetDefault(name, val)
flags.StringP(name, shorthand, val.Format(TimeFormat), description)
case []string:
m.viper.SetDefault(name, val)
flags.StringSliceP(name, shorthand, val, description)
case time.Duration:
m.viper.SetDefault(name, val)
flags.DurationP(name, shorthand, val, description)
case *time.Duration:
var def string
if val != nil {
def = fmt.Sprintf("%v", *val)
m.viper.SetDefault(name, def)
}
flags.StringP(name, shorthand, def, description)
case *tls.Config:
case map[string]string:
defs := make([]string, 0, len(val))
for k, v := range val {
defs = append(defs, fmt.Sprintf("%s=%v", k, v))
}
flags.StringSliceP(name, shorthand, defs, description)
m.viper.SetDefault(name, val)
case map[string][]byte:
defs := make([]string, 0, len(val))
for k, v := range val {
if len(v) > 0 {
defs = append(defs, fmt.Sprintf("%s=0x%X", k, v))
}
}
flags.StringSliceP(name, shorthand, defs, description)
m.viper.SetDefault(name, val)
case map[string][]string:
defs := make([]string, 0, len(val))
for k, vs := range val {
for _, v := range vs {
defs = append(defs, fmt.Sprintf("%s=%v", k, v))
}
}
flags.StringSliceP(name, shorthand, defs, description)
m.viper.SetDefault(name, val)
case []byte:
var str string
if len(val) > 0 {
str = fmt.Sprintf("0x%X", val)
m.viper.SetDefault(name, str)
}
flags.StringP(name, shorthand, str, description)
case types.NetID:
str := val.String()
m.viper.SetDefault(name, str)
flags.StringP(name, shorthand, str, description)
case types.EUI64:
str := val.String()
m.viper.SetDefault(name, str)
flags.StringP(name, shorthand, str, description)
case ttnpb.RxDelay:
m.viper.SetDefault(name, int32(val))
flags.Int32P(name, shorthand, int32(val), description)
case *ttnpb.RxDelay:
var def string
if val != nil {
def = fmt.Sprintf("%v", int32(*val))
m.viper.SetDefault(name, def)
}
flags.StringP(name, shorthand, def, description)
case *ttnpb.ADRAckDelayExponent:
var def string
if val != nil {
def = fmt.Sprintf("%v", int32(*val))
m.viper.SetDefault(name, def)
}
flags.StringP(name, shorthand, def, description)
case *ttnpb.ADRAckLimitExponent:
var def string
if val != nil {
def = fmt.Sprintf("%v", int32(*val))
m.viper.SetDefault(name, def)
}
flags.StringP(name, shorthand, def, description)
case *ttnpb.AggregatedDutyCycle:
var def string
if val != nil {
def = fmt.Sprintf("%v", int32(*val))
m.viper.SetDefault(name, def)
}
flags.StringP(name, shorthand, def, description)
case []RateLimitingProfile:
// Can only be set in the config file. Do not add command-line options
default:
fieldValue = reflect.Indirect(fieldValue)
switch fieldValue.Kind() {
case reflect.Struct:
if field.Anonymous {
name = prefix
}
m.setDefaults(name, flags, fieldValue.Interface())
case reflect.Invalid:
// TODO: Remove setDefaults, traverse reflect.Type instead of values directly.
fmt.Fprintf(os.Stderr, `config: skip "%v" in configuration at name "%s"`+"\n", field.Type, name)
m.viper.SetDefault(name, nil)
default:
panic(fmt.Errorf(`config: cannot work with "%v" in configuration at name "%s"`, field.Type, name))
}
}
}
}
}