-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
526 lines (442 loc) · 12.1 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
package configo
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"
"github.com/PaesslerAG/jsonpath"
"github.com/affanshahid/walkmap"
"github.com/imdario/mergo"
"github.com/spf13/cast"
)
const development = "dev"
type environment struct {
deployment, instance, shortHostname, fullHostname string
}
// Config is a hierarchical loader and access point for configurations.
// It allows loading configurations from mutiple files while being cognizant
// of the einvironment.
//
// Files are loading in the following order:
// default.EXT
// default-{instance}.EXT
// {deployment}.EXT
// {deployment}-{instance}.EXT
// {short_hostname}.EXT
// {short_hostname}-{instance}.EXT
// {short_hostname}-{deployment}.EXT
// {short_hostname}-{deployment}-{instance}.EXT
// {full_hostname}.EXT
// {full_hostname}-{instance}.EXT
// {full_hostname}-{deployment}.EXT
// {full_hostname}-{deployment}-{instance}.EXT
// local.EXT
// local-{instance}.EXT
// local-{deployment}.EXT
// local-{deployment}-{instance}.EXT
//
// EXT can be: `yaml`, `yml`, `json`, `json5`, `hjson`, `toml`
//
// deployment defines your current environment i.e dev, test, prod etc (defaults to "dev")
//
// instance can be the node ID in a multi-node deployment (defaults to "")
//
// shortHostname is the hostname till the first `.` (derived from `os.Hostname()` by default)
//
// fullHostname is the full host name (defaults to `os.Hostname()`)
//
// Each file overrides configurations from the file above.
// There is a special file called `env.EXT` which allows overriding
// configurations using environment variables
type Config struct {
environment
dir fs.FS
store map[string]interface{}
}
// ConfigOption is a functional option to configure a Config instance
type ConfigOption func(*Config)
// NewConfig creates a new Config
// dir is a FS of the directory containing the config files
// opts are functional options
func NewConfig(dir fs.FS, opts ...ConfigOption) (*Config, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
shortHostname := strings.Split(hostname, ".")[0]
c := &Config{dir: dir, environment: environment{development, "", shortHostname, hostname}}
for _, opt := range opts {
opt(c)
}
return c, nil
}
// WithDeployment sets the given deployment
func WithDeployment(deployment string) ConfigOption {
return func(c *Config) {
c.deployment = deployment
}
}
// WithInstance sets the given instance
func WithInstance(instance string) ConfigOption {
return func(c *Config) {
c.instance = instance
}
}
// WithHostname uses the given string to set shortHostname and fullHostname
func WithHostname(hostname string) ConfigOption {
return func(c *Config) {
c.shortHostname = strings.Split(hostname, ".")[0]
c.fullHostname = hostname
}
}
// WithDeploymentFromEnv loads the deployment label from the given environment variable
func WithDeploymentFromEnv(env string) ConfigOption {
deployment, exists := os.LookupEnv(env)
if exists {
return WithDeployment(deployment)
} else {
return WithDeployment(development)
}
}
// WithInstanceFromEnv loads the instance id from the given environment variable
func WithInstanceFromEnv(env string) ConfigOption {
return WithInstance(os.Getenv(env))
}
// WithHostnameFromEnv loads the hostname from the given environment variable
func WithHostnameFromEnv(env string) ConfigOption {
return WithHostname(os.Getenv(env))
}
// Initialize initializes and loads in the configurations
// This must be called before attempting to get values
func (c *Config) Initialize() error {
c.store = map[string]interface{}{}
files, err := fs.ReadDir(c.dir, ".")
if err != nil {
return err
}
fileMap := map[string]fs.DirEntry{}
for _, file := range files {
if file.IsDir() {
continue
}
nameWithoutExt := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
fileMap[nameWithoutExt] = file
}
for _, tmpl := range orderedTemplates {
filename := getExpectedBasename(tmpl, c.environment)
if filename == "" {
continue
}
entry, found := fileMap[filename]
if !found {
continue
}
data, err := c.readFile(entry.Name())
if err != nil {
return err
}
err = mergo.Merge(&c.store, data, mergo.WithOverride)
if err != nil {
return err
}
}
if envFile, found := fileMap[envFileName]; found {
data, err := c.readFile(envFile.Name())
if err != nil {
return err
}
err = c.loadOverrides(data)
if err != nil {
return err
}
}
return nil
}
func (c *Config) readFile(name string) (map[string]interface{}, error) {
in, err := fs.ReadFile(c.dir, name)
if err != nil {
return nil, err
}
ext := strings.ToLower(filepath.Ext(name))
provider := defaultProviders[ext]
return provider.Parse(in)
}
func (c *Config) loadOverrides(data map[string]interface{}) error {
var err error
walkmap.Walk(data, func(keyPath []interface{}, value interface{}, kind reflect.Kind) {
if err != nil {
return
}
strPath := make([]string, len(keyPath))
for i, p := range keyPath {
strPath[i] = p.(string)
}
envName, ok := value.(string)
if !ok {
err = fmt.Errorf("invalid environment variable %s", envName)
return
}
if envValue, found := os.LookupEnv(envName); found {
c.set(strPath, envValue)
}
})
return err
}
func (c *Config) set(paths []string, val interface{}) {
v := reflect.ValueOf(c.store)
for i := 0; i < len(paths)-1; i++ {
s := paths[i]
if v.Kind() == reflect.Interface {
v = v.Elem()
}
if i, err := strconv.Atoi(s); err == nil {
v = v.Index(i)
}
v = v.MapIndex(reflect.ValueOf(s))
}
if v.Kind() == reflect.Interface {
v = v.Elem()
}
v.SetMapIndex(reflect.ValueOf(paths[len(paths)-1]), reflect.ValueOf(val))
}
// Get returns the value at the given path as an interface
func (c *Config) Get(path string) (interface{}, error) {
return jsonpath.Get(path, c.store)
}
// GetString returns the value at the given path as a string
func (c *Config) GetString(path string) (string, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return "", err
}
return cast.ToStringE(out)
}
// GetBool returns the value at the given path as a boolean
func (c *Config) GetBool(path string) (bool, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return false, err
}
return cast.ToBoolE(out)
}
// GetInt returns the value at the given path as a int
func (c *Config) GetInt(path string) (int, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return 0, err
}
return cast.ToIntE(out)
}
// GetInt32 returns the value at the given path as a int32
func (c *Config) GetInt32(path string) (int32, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return 0, err
}
return cast.ToInt32E(out)
}
// GetInt64 returns the value at the given path as a int64
func (c *Config) GetInt64(path string) (int64, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return 0, err
}
return cast.ToInt64E(out)
}
// GetUint returns the value at the given path as a uint
func (c *Config) GetUint(path string) (uint, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return 0, err
}
return cast.ToUintE(out)
}
// GetUint32 returns the value at the given path as a uint32
func (c *Config) GetUint32(path string) (uint32, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return 0, err
}
return cast.ToUint32E(out)
}
// GetUint64 returns the value at the given path as a uint64
func (c *Config) GetUint64(path string) (uint64, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return 0, err
}
return cast.ToUint64E(out)
}
// GetFloat64 returns the value at the given path as a float64
func (c *Config) GetFloat64(path string) (float64, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return 0, err
}
return cast.ToFloat64E(out)
}
// GetTime returns the value at the given path as time
func (c *Config) GetTime(path string) (time.Time, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return time.Time{}, err
}
return cast.ToTimeE(out)
}
// GetDuration returns the value at the given path as a duration
func (c *Config) GetDuration(path string) (time.Duration, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return time.Duration(0), err
}
return cast.ToDurationE(out)
}
// GetIntSlice returns the value at the given path as a slice of int values
func (c *Config) GetIntSlice(path string) ([]int, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return nil, err
}
return cast.ToIntSliceE(out)
}
// GetStringSlice returns the value at the given path as a slice of string values
func (c *Config) GetStringSlice(path string) ([]string, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return nil, err
}
return cast.ToStringSliceE(out)
}
// GetStringMap returns the value at the given path as a map with string keys
// and values as interfaces
func (c *Config) GetStringMap(path string) (map[string]interface{}, error) {
out, err := jsonpath.Get(path, c.store)
if err != nil {
return nil, err
}
return cast.ToStringMapE(out)
}
// MustGet is the same as `Get` except it panics in case of an error
func (c *Config) MustGet(path string) interface{} {
v, err := c.Get(path)
if err != nil {
panic(err)
}
return v
}
// MustGetString is the same as `GetString` except it panics in case of an error
func (c *Config) MustGetString(path string) string {
v, err := c.GetString(path)
if err != nil {
panic(err)
}
return v
}
// MustGetBool is the same as `GetBool` except it panics in case of an error
func (c *Config) MustGetBool(path string) bool {
v, err := c.GetBool(path)
if err != nil {
panic(err)
}
return v
}
// MustGetInt is the same as `GetInt` except it panics in case of an error
func (c *Config) MustGetInt(path string) int {
v, err := c.GetInt(path)
if err != nil {
panic(err)
}
return v
}
// MustGetInt32 is the same as `GetInt32` except it panics in case of an error
func (c *Config) MustGetInt32(path string) int32 {
v, err := c.GetInt32(path)
if err != nil {
panic(err)
}
return v
}
// MustGetInt64 is the same as `GetInt64` except it panics in case of an error
func (c *Config) MustGetInt64(path string) int64 {
v, err := c.GetInt64(path)
if err != nil {
panic(err)
}
return v
}
// MustGetUint is the same as `GetUint` except it panics in case of an error
func (c *Config) MustGetUint(path string) uint {
v, err := c.GetUint(path)
if err != nil {
panic(err)
}
return v
}
// MustGetUint32 is the same as `GetUint32` except it panics in case of an error
func (c *Config) MustGetUint32(path string) uint32 {
v, err := c.GetUint32(path)
if err != nil {
panic(err)
}
return v
}
// MustGetUint64 is the same as `GetUint64` except it panics in case of an error
func (c *Config) MustGetUint64(path string) uint64 {
v, err := c.GetUint64(path)
if err != nil {
panic(err)
}
return v
}
// MustGetFloat64 is the same as `GetFloat64` except it panics in case of an error
func (c *Config) MustGetFloat64(path string) float64 {
v, err := c.GetFloat64(path)
if err != nil {
panic(err)
}
return v
}
// MustGetTime is the same as `GetTime` except it panics in case of an error
func (c *Config) MustGetTime(path string) time.Time {
v, err := c.GetTime(path)
if err != nil {
panic(err)
}
return v
}
// MustGetDuration is the same as `GetDuration` except it panics in case of an error
func (c *Config) MustGetDuration(path string) time.Duration {
v, err := c.GetDuration(path)
if err != nil {
panic(err)
}
return v
}
// MustGetIntSlice is the same as `GetIntSlice` except it panics in case of an error
func (c *Config) MustGetIntSlice(path string) []int {
v, err := c.GetIntSlice(path)
if err != nil {
panic(err)
}
return v
}
// MustGetStringSlice is the same as `GetStringSlice` except it panics in case of an error
func (c *Config) MustGetStringSlice(path string) []string {
v, err := c.GetStringSlice(path)
if err != nil {
panic(err)
}
return v
}
// MustGetStringMap is the same as `GetStringMap` except it panics in case of an error
func (c *Config) MustGetStringMap(path string) map[string]interface{} {
v, err := c.GetStringMap(path)
if err != nil {
panic(err)
}
return v
}