-
Notifications
You must be signed in to change notification settings - Fork 3
/
load.go
315 lines (271 loc) · 6.82 KB
/
load.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
package config
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/imdario/mergo"
"github.com/pkg/errors"
)
func LoadDir(dir, suffix string) error { return dc.LoadDir(dir, suffix) }
// loadDir
func (c *Config) LoadDir(dir, suffix string) (err error) {
fileList := []string{}
err = filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
ok := strings.HasSuffix(f.Name(), suffix)
if ok {
fileList = append(fileList, filepath.FromSlash(path))
}
return nil
})
if err != nil {
return
}
for _, file := range fileList {
if err = c.loadFile(file, false); err != nil {
return
}
}
return
}
// LoadFiles load one or multi files
func LoadFiles(sourceFiles ...string) error { return dc.LoadFiles(sourceFiles...) }
// LoadFiles load and parse config files
func (c *Config) LoadFiles(sourceFiles ...string) (err error) {
for _, file := range sourceFiles {
if err = c.loadFile(file, false); err != nil {
return
}
}
return
}
// LoadExists load one or multi files, will ignore not exist
func LoadExists(sourceFiles ...string) error { return dc.LoadExists(sourceFiles...) }
// LoadExists load and parse config files, but will ignore not exists file.
func (c *Config) LoadExists(sourceFiles ...string) (err error) {
for _, file := range sourceFiles {
if err = c.loadFile(file, true); err != nil {
return
}
}
return
}
// LoadRemote load config data from remote URL.
func LoadRemote(format, url string) error { return dc.LoadRemote(format, url) }
// LoadRemote load config data from remote URL.
// Usage:
// c.LoadRemote(config.JSON, "http://abc.com/api-config.json")
func (c *Config) LoadRemote(format, url string) (err error) {
// create http client
client := http.Client{Timeout: 300 * time.Second}
resp, err := client.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("fetch remote resource error, reply status code is not equals to 200")
}
// read response content
bts, err := ioutil.ReadAll(resp.Body)
if err == nil {
// parse file content
if err = c.parseSourceCode(format, bts); err != nil {
return
}
c.loadedFiles = append(c.loadedFiles, url)
}
return
}
// LoadOSEnv load data from OS ENV
func LoadOSEnv(keys []string) { dc.LoadOSEnv(keys) }
// LoadOSEnv load data from os ENV
func (c *Config) LoadOSEnv(keys []string) {
for _, key := range keys {
val := os.Getenv(strings.ToUpper(key))
_ = c.Set(key, val)
}
}
// support bound types for CLI flags vars
var validTypes = map[string]int{
"int": 1,
"uint": 1,
"bool": 1,
// string is default
"string": 1,
}
// LoadFlags load data from cli flags
func LoadFlags(keys []string) error { return dc.LoadFlags(keys) }
// LoadFlags parse command line arguments, based on provide keys.
// Usage:
// // debug flag is bool type
// c.LoadFlags([]string{"env", "debug:bool"})
func (c *Config) LoadFlags(keys []string) (err error) {
hash := map[string]interface{}{}
// bind vars
for _, key := range keys {
key, typ := parseVarNameAndType(key)
switch typ {
case "int":
ptr := new(int)
flag.IntVar(ptr, key, c.Int(key), "")
hash[key] = ptr
case "uint":
ptr := new(uint)
flag.UintVar(ptr, key, c.Uint(key), "")
hash[key] = ptr
case "bool":
ptr := new(bool)
flag.BoolVar(ptr, key, c.Bool(key), "")
hash[key] = ptr
default: // as string
ptr := new(string)
flag.StringVar(ptr, key, c.String(key), "")
hash[key] = ptr
}
}
// parse and collect
flag.Parse()
flag.Visit(func(f *flag.Flag) {
name := f.Name
// name := strings.Replace(f.Name, "-", ".", -1)
// only get name in the keys.
if _, ok := hash[name]; !ok {
return
}
// ignore error
_ = c.Set(name, f.Value.String())
})
return
}
func parseVarNameAndType(key string) (string, string) {
typ := "string"
key = strings.Trim(key, "-")
// can set var type: int, uint, bool
if strings.IndexByte(key, ':') > 0 {
list := strings.SplitN(key, ":", 2)
key, typ = list[0], list[1]
if _, ok := validTypes[typ]; !ok {
typ = "string"
}
}
return key, typ
}
// LoadData load one or multi data
func LoadData(dataSource ...interface{}) error { return dc.LoadData(dataSource...) }
// LoadData load data from map OR struct
func (c *Config) LoadData(dataSources ...interface{}) (err error) {
for _, ds := range dataSources {
err = mergo.Merge(&c.data, ds, mergo.WithOverride)
if err != nil {
return
}
}
return
}
// LoadSources load one or multi byte data
func LoadSources(format string, src []byte, more ...[]byte) error {
return dc.LoadSources(format, src, more...)
}
// LoadSources load data from byte content.
// Usage:
// config.LoadSources(config.Yml, []byte(`
// name: blog
// arr:
// key: val
// `))
func (c *Config) LoadSources(format string, src []byte, more ...[]byte) (err error) {
err = c.parseSourceCode(format, src)
if err != nil {
return
}
for _, sc := range more {
err = c.parseSourceCode(format, sc)
if err != nil {
return
}
}
return
}
// LoadStrings load one or multi string
func LoadStrings(format string, str string, more ...string) error {
return dc.LoadStrings(format, str, more...)
}
// LoadStrings load data from source string content.
func (c *Config) LoadStrings(format string, str string, more ...string) (err error) {
err = c.parseSourceCode(format, []byte(str))
if err != nil {
return
}
for _, s := range more {
err = c.parseSourceCode(format, []byte(s))
if err != nil {
return
}
}
return
}
// load config file
func (c *Config) loadFile(file string, loadExist bool) (err error) {
// open file
fd, err := os.Open(file)
if err != nil {
// skip not exist file
if os.IsNotExist(err) && loadExist {
return nil
}
return err
}
defer fd.Close()
// read file content
bts, err := ioutil.ReadAll(fd)
if err == nil {
// get format for file ext
format := strings.Trim(filepath.Ext(file), ".")
// parse file content
if err = c.parseSourceCode(format, bts); err != nil {
return
}
c.loadedFiles = append(c.loadedFiles, file)
}
return
}
// parse config source code to Config.
func (c *Config) parseSourceCode(format string, blob []byte) (err error) {
var ok bool
var decoder Decoder
switch format {
case Hcl:
decoder, ok = c.decoders[Hcl]
case Ini:
decoder, ok = c.decoders[Ini]
case JSON:
decoder, ok = c.decoders[JSON]
case Yaml, Yml:
decoder, ok = c.decoders[Yaml]
case Toml:
decoder, ok = c.decoders[Toml]
}
if !ok {
return errors.New("no exists or no register decoder for the format: " + format)
}
data := make(map[string]interface{})
// decode content to data
if err = decoder(blob, &data); err != nil {
return
}
// init config data
if len(c.data) == 0 {
c.data = data
} else {
// again ... will merge data
// err = mergo.Map(&c.data, data, mergo.WithOverride)
err = mergo.Merge(&c.data, data, mergo.WithOverride)
}
data = nil
return
}