/
config.go
517 lines (485 loc) · 12.3 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// Copyright 2015 Globo.com. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package config provide configuration facilities, handling configuration
// files in yaml format.
//
// This package has been optimized for reads, so functions write functions (Set
// and Unset) are really slow when compared to Get functions.
package config
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/howeyc/fsnotify"
"gopkg.in/yaml.v1"
)
var ErrMismatchConf = errors.New("Your conf is wrong:")
var Mod string
type configuration struct {
data map[interface{}]interface{}
sync.RWMutex
}
func (c *configuration) Store(data map[interface{}]interface{}) {
c.Lock()
defer c.Unlock()
c.store(data)
}
func (c *configuration) store(data map[interface{}]interface{}) {
c.data = data
}
func (c *configuration) Data() map[interface{}]interface{} {
c.RLock()
defer c.RUnlock()
return c.data
}
var configs configuration
func readConfigBytes(data []byte, out interface{}) error {
return yaml.Unmarshal(data, out)
}
// ReadConfigBytes receives a slice of bytes and builds the internal
// configuration object.
//
// If the given slice is not a valid yaml file, ReadConfigBytes returns a
// non-nil error.
func ReadConfigBytes(data []byte) error {
var newConfig map[interface{}]interface{}
err := readConfigBytes(data, &newConfig)
if err == nil {
configs.Store(newConfig)
}
return err
}
// ReadConfigFile reads the content of a file and calls ReadConfigBytes to
// build the internal configuration object.
//
// It returns error if it can not read the given file or if the file contents
// is not valid yaml.
func ReadConfigFile(filePath string) error {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
return ReadConfigBytes(data)
}
// ReadAndWatchConfigFile reads and watchs for changes in the configuration
// file. Whenever the file change, and its contents are valid YAML, the
// configuration gets updated. With this function, daemons that use this
// package may reload configuration without restarting.
func ReadAndWatchConfigFile(filePath string) error {
err := ReadConfigFile(filePath)
if err != nil {
return err
}
w, err := fsnotify.NewWatcher()
if err != nil {
return err
}
err = w.Watch(filePath)
if err != nil {
return err
}
go func() {
for {
select {
case e := <-w.Event:
if e.IsModify() {
ReadConfigFile(filePath)
}
case <-w.Error: // just ignore errors
}
}
}()
return nil
}
// Bytes serialize the configuration in YAML format.
func Bytes() ([]byte, error) {
return yaml.Marshal(configs.Data())
}
// WriteConfigFile writes the configuration to the disc, using the given path.
// The configuration is serialized in YAML format.
//
// This function will create the file if it does not exist, setting permissions
// to "perm".
func WriteConfigFile(filePath string, perm os.FileMode) error {
b, err := Bytes()
if err != nil {
return err
}
f, err := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_EXCL, perm)
if err != nil {
return err
}
defer f.Close()
n, err := f.Write(b)
if err != nil {
return err
}
if n != len(b) {
return io.ErrShortWrite
}
return nil
}
// Get returns the value for the given key, or an error if the key is undefined.
//
// The key is composed of all the key names separated by :, in case of nested
// keys. For example, suppose we have the following configuration yaml:
//
// databases:
// mysql:
// host: localhost
// port: 3306
//
// The key "databases:mysql:host" would return "localhost", while the key
// "port" would return an error.
//
// Get will expand the value with environment values, ex.:
//
// mongo: $MONGOURI
//
// If there is an environment variable MONGOURI=localhost/test, the key "mongo"
// would return "localhost/test". If the variable value is a json object/list, this
// object will also be expanded.
func Get(key string) (interface{}, error) {
if Mod == "" {
Mod = "dev"
}
key = Mod + ":" + key
keys := strings.Split(key, ":")
configs.RLock()
defer configs.RUnlock()
conf, ok := configs.data[keys[0]]
if !ok {
return nil, fmt.Errorf("key %q not found", key)
}
for _, k := range keys[1:] {
switch c := conf.(type) {
case map[interface{}]interface{}:
if conf, ok = c[k]; !ok {
return nil, fmt.Errorf("key %q not found", key)
}
case string:
value, err := expandEnv(c)
if err != nil {
return nil, ErrMismatchConf
}
if conf, ok = value.(map[interface{}]interface{})[k]; !ok {
return nil, fmt.Errorf("key %q not found", key)
}
default:
return nil, ErrMismatchConf
}
}
if v, ok := conf.(func() interface{}); ok {
conf = v()
}
if v, ok := conf.(string); ok {
value, _ := expandEnv(v)
return value, nil
}
return conf, nil
}
// expandEnv expands an environment variable and unmarshalls
// an json object or slice if it's found.
func expandEnv(s string) (interface{}, error) {
raw := os.ExpandEnv(s)
var jsonMap map[string]interface{}
err := json.Unmarshal([]byte(raw), &jsonMap)
if err != nil {
var jsonSlice []interface{}
errS := json.Unmarshal([]byte(raw), &jsonSlice)
if errS != nil {
return raw, err
}
return jsonSlice, nil
}
return toInfMap(jsonMap), nil
}
// toInfMap takes an map[string]interface{} and recursively converts it
// to an map[interface{}]interface{}.
func toInfMap(sMap map[string]interface{}) map[interface{}]interface{} {
newMap := make(map[interface{}]interface{})
for k, v := range sMap {
var newValue interface{}
switch v := v.(type) {
case map[string]interface{}:
newValue = toInfMap(v)
default:
newValue = v
}
newMap[k] = newValue
}
return newMap
}
// GetString works like Get, but does an string type assertion before returning
// the value.
//
// It returns error if the key is undefined or if it is not a string.
func GetString(key string) (string, error) {
value, err := Get(key)
if err != nil {
return "", err
}
switch v := value.(type) {
case int:
return strconv.Itoa(v), nil
case int64:
return strconv.FormatInt(v, 10), nil
default:
if v, ok := value.(string); ok {
return v, nil
}
}
return "", &invalidValue{key, "string|int|int64"}
}
// GetStringDefault works like Get
//
// It returns default if the key is undefined or if it is not a string.
func GetStringDefault(key, defaul string) string {
res, err := GetString(key)
if err != nil {
res = defaul
}
return res
}
// GetInt works like Get, but does an int type assertion and attempts string
// conversion before returning the value.
//
// It returns error if the key is undefined or if it is not a int.
func GetInt(key string) (int, error) {
value, err := Get(key)
if err != nil {
return 0, err
}
if v, ok := value.(int); ok {
return v, nil
} else if v, ok := value.(string); ok {
if i, err := strconv.ParseInt(v, 10, 64); err == nil {
return int(i), nil
}
} else if v, err := GetFloat(key); err == nil {
if float64(int(v)) == v {
return int(v), nil
}
}
return 0, &invalidValue{key, "int"}
}
// GetIntDefault works like Get
//
// It returns default if the key is undefined or if it is not a int.
func GetIntDefault(key string, defaul int) int {
res, err := GetInt(key)
if err != nil {
res = defaul
}
return res
}
// GetFloat works like Get, but does a float type assertion and attempts string
// conversion before returning the value.
//
// It returns error if the key is undefined or if it is not a float.
func GetFloat(key string) (float64, error) {
value, err := Get(key)
if err != nil {
return 0, err
}
switch v := value.(type) {
case float64:
return v, nil
case float32:
return float64(v), nil
case int:
return float64(v), nil
case string:
if floatVal, err := strconv.ParseFloat(v, 64); err == nil {
return floatVal, nil
}
}
return 0, &invalidValue{key, "float"}
}
// GetUint parses and returns an unsigned integer from the config file.
func GetUint(key string) (uint, error) {
if v, err := GetInt(key); err == nil {
if v < 0 {
return 0, &invalidValue{key, "uint"}
}
return uint(v), nil
}
return 0, &invalidValue{key, "uint"}
}
// GetDuration parses and returns a duration from the config file. It may be an
// integer or a number specifying the amount of nanoseconds.
//
// Here are some examples of valid durations:
//
// - 1h30m0s
// - 1e9 (one second)
// - 100e6 (one hundred milliseconds)
// - 1 (one nanosecond)
// - 1000000000 (one billion nanoseconds, or one second)
func GetDuration(key string) (time.Duration, error) {
value, err := Get(key)
if err != nil {
return 0, err
}
switch v := value.(type) {
case int:
return time.Duration(v), nil
case float64:
return time.Duration(v), nil
case string:
if value, err := time.ParseDuration(value.(string)); err == nil {
return value, nil
}
}
return 0, &invalidValue{key, "duration"}
}
// GetBool does a type assertion before returning the requested value
func GetBool(key string) (bool, error) {
value, err := Get(key)
if err != nil {
return false, err
}
if v, ok := value.(bool); ok {
return v, nil
}
return false, &invalidValue{key, "boolean"}
}
// GetList works like Get, but returns a slice of strings instead. It must be
// written down in the config as YAML lists.
//
// Here are two example of YAML lists:
//
// names:
// - Mary
// - John
// - Paul
// - Petter
//
// If GetList find an item that is not a string (for example 5.08734792), it
// will convert the item.
func GetList(key string) ([]string, error) {
value, err := Get(key)
if err != nil {
return nil, err
}
switch value.(type) {
case []interface{}:
v := value.([]interface{})
result := make([]string, len(v))
for i, item := range v {
switch v := item.(type) {
case int:
result[i] = strconv.Itoa(v)
case bool:
result[i] = strconv.FormatBool(v)
case float64:
result[i] = strconv.FormatFloat(v, 'f', -1, 64)
case string:
result[i] = v
default:
result[i] = fmt.Sprintf("%v", item)
}
}
return result, nil
case []string:
return value.([]string), nil
}
return nil, &invalidValue{key, "list"}
}
// mergeMaps takes two maps and merge its keys and values recursively.
//
// In case of conflicts, the function picks value from map2.
func mergeMaps(map1, map2 map[interface{}]interface{}) map[interface{}]interface{} {
result := make(map[interface{}]interface{})
for k, v2 := range map2 {
if v1, ok := map1[k]; !ok {
result[k] = v2
} else {
map1, ok1 := v1.(map[interface{}]interface{})
map2, ok2 := v2.(map[interface{}]interface{})
if ok1 && ok2 {
result[k] = mergeMaps(map1, map2)
} else {
result[k] = v2
}
}
}
for k, v := range map1 {
if v2, ok := map2[k]; !ok {
result[k] = v
} else {
map1, ok1 := v.(map[interface{}]interface{})
map2, ok2 := v2.(map[interface{}]interface{})
if ok1 && ok2 {
result[k] = mergeMaps(map1, map2)
}
}
}
return result
}
// Set redefines or defines a value for a key. The key has the same format that
// it has in Get and GetString.
//
// Values defined by this function affects only runtime informatin, nothing
// defined by Set is persisted in the filesystem or any database.
func Set(key string, value interface{}) {
parts := strings.Split(key, ":")
last := map[interface{}]interface{}{
parts[len(parts)-1]: value,
}
for i := len(parts) - 2; i >= 0; i-- {
last = map[interface{}]interface{}{
parts[i]: last,
}
}
configs.Lock()
defer configs.Unlock()
configs.store(mergeMaps(configs.data, last))
}
// Unset removes a key from the configuration map. It returns an error if the
// key is not defined.
//
// Calling this function does not remove a key from a configuration file, only
// from the in-memory configuration object.
func Unset(key string) error {
var i int
var part string
configs.Lock()
defer configs.Unlock()
data := configs.data
m := make(map[interface{}]interface{}, len(data))
for k, v := range data {
m[k] = v
}
root := m
parts := strings.Split(key, ":")
for i, part = range parts {
if item, ok := m[part]; ok {
if nm, ok := item.(map[interface{}]interface{}); ok && i < len(parts)-1 {
m = nm
} else {
break
}
} else {
return fmt.Errorf("key %q not found", key)
}
}
delete(m, part)
configs.store(root)
return nil
}
type invalidValue struct {
key string
kind string
}
func (e *invalidValue) Error() string {
return fmt.Sprintf("value for the key %q is not a %s", e.key, e.kind)
}