/
config.go
173 lines (138 loc) · 4.33 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
package configutils
import (
"bytes"
"fmt"
"io/ioutil"
"reflect"
"github.com/koofr/envigo"
yaml "gopkg.in/yaml.v3"
)
type LoadConfigOptions struct {
EnvOverride bool
EnvPrefix string
EnvGetter envigo.EnvGetter
OverrideConfigFiles []string
OverrideConfigBytes [][]byte
YAMLValidateKeys bool
YAMLPatchBytes func(b []byte) []byte
}
func DisableEnvOverride() func(*LoadConfigOptions) {
return func(opts *LoadConfigOptions) {
opts.EnvOverride = false
}
}
func EnvPrefix(envPrefix string) func(*LoadConfigOptions) {
return func(opts *LoadConfigOptions) {
opts.EnvPrefix = envPrefix
}
}
func EnvGetter(envGetter envigo.EnvGetter) func(*LoadConfigOptions) {
return func(opts *LoadConfigOptions) {
opts.EnvGetter = envGetter
}
}
func OverrideConfigFile(configFile string) func(*LoadConfigOptions) {
return func(opts *LoadConfigOptions) {
if configFile != "" {
opts.OverrideConfigFiles = append(opts.OverrideConfigFiles, configFile)
}
}
}
func OverrideConfigBytes(configBytes []byte) func(*LoadConfigOptions) {
return func(opts *LoadConfigOptions) {
opts.OverrideConfigBytes = append(opts.OverrideConfigBytes, configBytes)
}
}
func YAMLValidateKeys(validate bool) func(*LoadConfigOptions) {
return func(opts *LoadConfigOptions) {
opts.YAMLValidateKeys = validate
}
}
func YAMLPatchBytes(patch func(b []byte) []byte) func(*LoadConfigOptions) {
return func(opts *LoadConfigOptions) {
opts.YAMLPatchBytes = patch
}
}
func LoadConfigFile(configFile string, config interface{}) (err error) {
configBytes, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
return yaml.Unmarshal(configBytes, config)
}
func loadConfigFileOpts(configFile string, config interface{}, opts *LoadConfigOptions) (err error) {
configBytes, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
return loadConfigBytesOpts(configBytes, config, opts)
}
func loadConfigBytesOpts(configBytes []byte, config interface{}, opts *LoadConfigOptions) (err error) {
if opts.YAMLPatchBytes != nil {
configBytes = opts.YAMLPatchBytes(configBytes)
}
decoder := yaml.NewDecoder(bytes.NewReader(configBytes))
if opts.YAMLValidateKeys {
decoder.KnownFields(true)
}
return decoder.Decode(config)
}
func getOpts(optFuncs ...func(*LoadConfigOptions)) *LoadConfigOptions {
opts := &LoadConfigOptions{
EnvOverride: true,
EnvPrefix: "",
EnvGetter: envigo.EnvironGetter(),
OverrideConfigFiles: nil,
OverrideConfigBytes: nil,
YAMLValidateKeys: true,
YAMLPatchBytes: nil,
}
for _, optFunc := range optFuncs {
optFunc(opts)
}
return opts
}
func applyOpts(config interface{}, opts *LoadConfigOptions) error {
for _, overrideConfigFile := range opts.OverrideConfigFiles {
if err := loadConfigFileOpts(overrideConfigFile, config, opts); err != nil {
return fmt.Errorf("override error: %w", err)
}
}
for _, overrideConfigBytes := range opts.OverrideConfigBytes {
if err := loadConfigBytesOpts(overrideConfigBytes, config, opts); err != nil {
return fmt.Errorf("override error: %w", err)
}
}
if opts.EnvOverride {
if err := envigo.Envigo(config, opts.EnvPrefix, opts.EnvGetter); err != nil {
return fmt.Errorf("envigo error: %w", err)
}
}
return nil
}
func LoadConfig(configFile string, config interface{}, optFuncs ...func(*LoadConfigOptions)) (err error) {
opts := getOpts(optFuncs...)
for reflect.ValueOf(config).Kind() == reflect.Ptr && reflect.ValueOf(config).Elem().Kind() == reflect.Ptr {
config = reflect.ValueOf(config).Elem().Interface()
}
if err := loadConfigFileOpts(configFile, config, opts); err != nil {
return fmt.Errorf("LoadConfig error: %w", err)
}
if err := applyOpts(config, opts); err != nil {
return fmt.Errorf("LoadConfig error: %w", err)
}
return nil
}
func LoadConfigBytes(configFileBytes []byte, config interface{}, optFuncs ...func(*LoadConfigOptions)) (err error) {
opts := getOpts(optFuncs...)
for reflect.ValueOf(config).Kind() == reflect.Ptr && reflect.ValueOf(config).Elem().Kind() == reflect.Ptr {
config = reflect.ValueOf(config).Elem().Interface()
}
if err = loadConfigBytesOpts(configFileBytes, config, opts); err != nil {
return fmt.Errorf("LoadConfigBytes error: %w", err)
}
if err := applyOpts(config, opts); err != nil {
return fmt.Errorf("LoadConfigBytes error: %w", err)
}
return nil
}