forked from liamjbennett/sous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configloader.go
180 lines (163 loc) · 4.37 KB
/
configloader.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
// The configloader package provides YAML-based configuration files, with
// automatic environment variable overriding.
package configloader
import (
"fmt"
"io/ioutil"
"os"
"reflect"
"strconv"
"strings"
"github.com/opentable/sous/util/yaml"
)
func New() *configLoader {
return &configLoader{}
}
type (
// ConfigLoader loads configuration.
ConfigLoader interface {
//Loads a YAML formated configuration from path into data.
Load(data interface{}, path string) error
}
DefaultFiller interface {
FillDefaults() error
}
Validator interface {
Validate() error
}
configLoader struct {
// Log is called with debug level logs about how values are resolved.
Debug, Info func(...interface{})
}
)
// SetLogFunc implements sous.ILogger on configLoader
func (cl *configLoader) SetLogFunc(f func(...interface{})) {
cl.Info = f
}
// SetDebugFunc implements sous.ILogger on configLoader
func (cl *configLoader) SetDebugFunc(f func(...interface{})) {
cl.Debug = f
}
func (cl *configLoader) Load(target interface{}, filePath string) error {
if target == nil {
return fmt.Errorf("target was nil, need a value")
}
_, err := os.Stat(filePath)
if err != nil {
if !os.IsNotExist(err) {
return err
}
//cl.Info("Missing config file, using defaults", map[string]interface{}{"path": filePath})
} else {
if err := cl.loadYAMLFile(target, filePath); err != nil {
return err
}
}
if fd, ok := target.(DefaultFiller); ok {
if err := fd.FillDefaults(); err != nil {
return err
}
}
if err := cl.overrideWithEnv(target); err != nil {
return err
}
return nil
}
func (cl *configLoader) Validate(target interface{}) error {
if validator, ok := target.(Validator); ok {
return validator.Validate()
}
return nil
}
func (cl *configLoader) overrideWithEnv(target interface{}) error {
return cl.forEachField(target, cl.overrideField)
}
func (cl *configLoader) forEachField(target interface{}, f func(field reflect.StructField, val reflect.Value) error) error {
v := reflect.ValueOf(target)
if v.Kind() != reflect.Ptr && v.Elem().Kind() != reflect.Struct {
return fmt.Errorf("target was %T; need a pointer to struct", target)
}
v = v.Elem()
t := v.Type()
for i := 0; i < v.NumField(); i++ {
if err := f(t.Field(i), v.Field(i)); err != nil {
return err
}
}
return nil
}
func (cl *configLoader) forFieldNamed(target interface{}, name string, f func(field reflect.StructField, val reflect.Value) error) error {
found := false
err := cl.forEachField(target, func(field reflect.StructField, val reflect.Value) error {
if strings.ToLower(field.Name) == strings.ToLower(name) {
found = true
return f(field, val)
}
return nil
})
if !found {
return fmt.Errorf("config value %s does not exist", name)
}
return err
}
func (cl *configLoader) GetValue(from interface{}, name string) (interface{}, error) {
var x interface{}
return x, cl.forFieldNamed(from, name, func(field reflect.StructField, val reflect.Value) error {
if field.Type.Kind() != reflect.Ptr || !val.IsNil() {
x = val.Interface()
}
return nil
})
}
func (cl *configLoader) SetValue(target interface{}, name, value string) error {
return cl.forFieldNamed(target, name, func(field reflect.StructField, val reflect.Value) error {
switch k := field.Type.Kind(); k {
default:
return fmt.Errorf("configloader does not know how to set fields of kind %s", k)
case reflect.String:
val.Set(reflect.ValueOf(value))
case reflect.Int:
v, err := strconv.Atoi(value)
if err != nil {
return err
}
val.Set(reflect.ValueOf(v))
}
return nil
})
}
func (cl *configLoader) overrideField(sf reflect.StructField, originalVal reflect.Value) error {
envName := sf.Tag.Get("env")
if envName == "" {
return nil
}
envVal, present := os.LookupEnv(envName)
if !present {
return nil
}
var finalVal reflect.Value
switch originalVal.Interface().(type) {
default:
return fmt.Errorf("unable to override fields of type %T", originalVal.Interface())
case string:
finalVal = reflect.ValueOf(envVal)
case int:
i, err := strconv.Atoi(envVal)
if err != nil {
return err
}
finalVal = reflect.ValueOf(i)
}
originalVal.Set(finalVal)
return nil
}
func (cl *configLoader) loadYAMLFile(target interface{}, filePath string) error {
if filePath == "" {
return fmt.Errorf("filepath was empty")
}
b, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
return yaml.Unmarshal(b, target)
}