This repository has been archived by the owner on Apr 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
266 lines (223 loc) · 6.73 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
package sprbox
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
"text/template"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
// struct field flags
const (
// sffEnv value can be in json format, it will override also the default value
sffEnv = "env"
// set the default value
sffDefault = "default"
// return error if missing value
sffRequired = "required"
)
// files type regexp
const (
regexYAML = `(?i)(.y(|a)ml)`
regexTOML = `(?i)(.toml)`
regexJSON = `(?i)(.json)`
)
func unmarshalJSON(data []byte, config interface{}, filePath string) (err error) {
return json.Unmarshal(data, config)
}
func unmarshalTOML(data []byte, config interface{}, filePath string) (err error) {
_, err = toml.Decode(string(data), config)
return err
}
func unmarshalYAML(data []byte, config interface{}, filePath string) (err error) {
return yaml.Unmarshal(data, config)
}
// parseConfigTags will process the struct field tags.
func parseConfigTags(elem interface{}, indent string) error {
elemValue := reflect.Indirect(reflect.ValueOf(elem))
switch elemValue.Kind() {
case reflect.Struct:
elemType := elemValue.Type()
verbosePrintf("%sProcessing STRUCT: %s = %+v\n", indent, elemType.Name(), elem)
for i := 0; i < elemType.NumField(); i++ {
ft := elemType.Field(i)
fv := elemValue.Field(i)
if !fv.CanAddr() || !fv.CanInterface() {
verbosePrintf("%sCan't addr or interface FIELD: CanAddr: %v, CanInterface: %v. -> %s = '%+v'\n",
indent, fv.CanAddr(), fv.CanInterface(), ft.Name, fv.Interface())
continue
}
tag := ft.Tag.Get(sftKey)
tagFields := strings.Split(tag, ",")
verbosePrintf("\n%sProcessing FIELD: %s %s = %+v, tags: %s\n",
indent, ft.Name, ft.Type.String(), fv.Interface(), tag)
for _, flag := range tagFields {
kv := strings.Split(flag, "=")
if kv[0] == sffEnv {
if len(kv) == 2 {
if value := os.Getenv(kv[1]); len(value) > 0 {
debugPrintf("Loading configuration for struct `%v`'s field `%v` from env %v...\n",
elemType.Name(), ft.Name, kv[1])
if err := yaml.Unmarshal([]byte(value), fv.Addr().Interface()); err != nil {
return err
}
}
}
}
if empty := reflect.DeepEqual(fv.Interface(), reflect.Zero(fv.Type()).Interface()); empty {
if kv[0] == sffDefault {
if len(kv) == 2 {
if err := yaml.Unmarshal([]byte(kv[1]), fv.Addr().Interface()); err != nil {
return err
}
}
} else if kv[0] == sffRequired {
return errors.New(ft.Name + " is required")
}
}
}
switch fv.Kind() {
case reflect.Ptr, reflect.Struct, reflect.Slice, reflect.Map:
if err := parseConfigTags(fv.Addr().Interface(), " "); err != nil {
return err
}
}
verbosePrintf("%sProcessed FIELD: %s %s = %+v\n", indent, ft.Name, ft.Type.String(), fv.Interface())
}
case reflect.Slice:
for i := 0; i < elemValue.Len(); i++ {
if err := parseConfigTags(elemValue.Index(i).Addr().Interface(), " "); err != nil {
return err
}
}
case reflect.Map:
for _, key := range elemValue.MapKeys() {
if err := parseConfigTags(elemValue.MapIndex(key).Interface(), " "); err != nil {
return err
}
}
}
return nil
}
// parseTemplateBytes parse all text/template placeholders
// (eg.: {{.Key}}) in config files.
func parseTemplateBytes(file []byte, config interface{}) error {
var buf bytes.Buffer
var tpl *template.Template
var err error
if tpl, err = template.New("tpl").Parse(string(file)); err != nil {
return err
}
if err = tpl.Execute(&buf, config); err != nil {
return err
}
switch {
case unmarshalJSON(buf.Bytes(), config, "") == nil:
return nil
case unmarshalYAML(buf.Bytes(), config, "") == nil:
return nil
case unmarshalTOML(buf.Bytes(), config, "") == nil:
return nil
default:
return fmt.Errorf("the provided data is incompatible with an interface of type %T:\n%s",
config, strings.TrimSuffix(string(file), "\n"))
}
}
// parseTemplateFile parse all text/template placeholders
// (eg.: {{.Key}}) in config files.
func parseTemplateFile(file string, config interface{}) error {
var buf bytes.Buffer
var tpl *template.Template
var err error
if tpl, err = template.ParseFiles(file); err != nil {
return err
}
if err = tpl.Execute(&buf, config); err != nil {
return err
}
ext := filepath.Ext(file)
switch {
case regexp.MustCompile(regexYAML).MatchString(ext):
return unmarshalYAML(buf.Bytes(), config, file)
case regexp.MustCompile(regexTOML).MatchString(ext):
return unmarshalTOML(buf.Bytes(), config, file)
case regexp.MustCompile(regexJSON).MatchString(ext):
return unmarshalJSON(buf.Bytes(), config, file)
default:
return fmt.Errorf("unknown data format, can't unmarshal file: '%s'", file)
}
}
// Unmarshal will unmarshal []byte to interface
// for yaml, toml and json data formats.
//
// Will also parse struct flags.
func Unmarshal(data []byte, config interface{}) (err error) {
switch {
case unmarshalJSON(data, config, "") == nil:
break
case unmarshalYAML(data, config, "") == nil:
break
case unmarshalTOML(data, config, "") == nil:
break
default:
return fmt.Errorf("the provided data is incompatible with an interface of type %T:\n%s",
config, strings.TrimSuffix(string(data), "\n"))
}
//debugPrintf("elem: %s\n%+v\n", string(data), config)
if err = parseTemplateBytes(data, config); err != nil {
return err
}
return parseConfigTags(config, "")
}
// LoadConfig will unmarshal all the matched
// config files to the config interface.
//
// Build-environment specific files will override generic files.
// The latest files will override the earliest.
//
// Will also parse struct flags.
func LoadConfig(config interface{}, files ...string) (err error) {
foundFiles := configFilesByEnv(files...)
if len(foundFiles) == 0 {
return fmt.Errorf("no config file found for '%s'", strings.Join(files, " | "))
}
for _, file := range foundFiles {
var in []byte
if in, err = ioutil.ReadFile(file); err != nil {
return err
}
ext := filepath.Ext(file)
switch {
case regexp.MustCompile(regexYAML).MatchString(ext):
err = unmarshalYAML(in, config, file)
case regexp.MustCompile(regexTOML).MatchString(ext):
err = unmarshalTOML(in, config, file)
case regexp.MustCompile(regexJSON).MatchString(ext):
err = unmarshalJSON(in, config, file)
default:
err = fmt.Errorf("unknown data format, can't unmarshal file: '%s'", file)
}
if err != nil {
return err
}
if err = parseTemplateFile(file, config); err != nil {
return err
}
}
//configB, err := yaml.Marshal(config)
//if err != nil {
// return err
//}
//if err = parseTemplate(configB, config); err != nil {
// return err
//}
defer debugPrintf("%s\n", green(dump(config)))
return parseConfigTags(config, "")
}