-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
65 lines (54 loc) · 1.38 KB
/
parse.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
package yacl
import (
"github.com/andrew528i/yacl/env"
"github.com/andrew528i/yacl/file"
"github.com/andrew528i/yacl/flags"
"github.com/andrew528i/yacl/utils"
)
func Parse[T any](defaultConfigs ...*T) (*T, error) {
var cfg T
// First use default configs if they provided
for _, defaultConfig := range defaultConfigs {
utils.MergeStruct(&cfg, defaultConfig)
}
// Merge config values from yaml file
yamlCfg, err := file.ParseYAML[T](file.DefaultParams())
if err != nil {
if _, ok := err.(*file.NotFound); !ok {
return nil, err
}
} else {
utils.MergeStruct(&cfg, yamlCfg)
}
// Merge config values from json file
jsonCfg, err := file.ParseJSON[T](file.DefaultParams())
if err != nil {
if _, ok := err.(*file.NotFound); !ok {
return nil, err
}
} else {
utils.MergeStruct(&cfg, jsonCfg)
}
// Merge config values from json file
binaryCfg, err := file.ParseBinary[T](file.DefaultParams())
if err != nil {
if _, ok := err.(*file.NotFound); !ok {
return nil, err
}
} else {
utils.MergeStruct(&cfg, binaryCfg)
}
// Merge config values from env
envCfg, err := env.Parse[T](env.DefaultParams())
if err != nil {
return nil, err
}
utils.MergeStruct(&cfg, envCfg)
// Merge config values from command line interface
flagCfg, err := flags.Parse[T](flags.DefaultParams())
if err != nil {
return nil, err
}
utils.MergeStruct(&cfg, flagCfg)
return &cfg, nil
}