forked from hyperledger-archives/burrow
/
config.go
95 lines (83 loc) · 2.49 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
package config
import (
"bufio"
"fmt"
"os"
"strings"
"sync"
"time"
)
func Prompt(prompt string, defaultValue string) string {
fmt.Print(prompt)
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
if err != nil {
log.Warn("Error reading stdin", "err", err)
return defaultValue
} else {
line = strings.TrimSpace(line)
if line == "" {
return defaultValue
}
return line
}
}
type Config interface {
Get(key string) interface{}
GetBool(key string) bool
GetFloat64(key string) float64
GetInt(key string) int
GetString(key string) string
GetStringMap(key string) map[string]interface{}
GetStringMapString(key string) map[string]string
GetStringSlice(key string) []string
GetTime(key string) time.Time
IsSet(key string) bool
Set(key string, value interface{})
}
type MapConfig map[string]interface{}
func (cfg MapConfig) Get(key string) interface{} { return cfg[key] }
func (cfg MapConfig) GetBool(key string) bool { return cfg[key].(bool) }
func (cfg MapConfig) GetFloat64(key string) float64 { return cfg[key].(float64) }
func (cfg MapConfig) GetInt(key string) int { return cfg[key].(int) }
func (cfg MapConfig) GetString(key string) string { return cfg[key].(string) }
func (cfg MapConfig) GetStringMap(key string) map[string]interface{} {
return cfg[key].(map[string]interface{})
}
func (cfg MapConfig) GetStringMapString(key string) map[string]string {
return cfg[key].(map[string]string)
}
func (cfg MapConfig) GetStringSlice(key string) []string { return cfg[key].([]string) }
func (cfg MapConfig) GetTime(key string) time.Time { return cfg[key].(time.Time) }
func (cfg MapConfig) IsSet(key string) bool { _, ok := cfg[key]; return ok }
func (cfg MapConfig) Set(key string, value interface{}) { cfg[key] = value }
func (cfg MapConfig) SetDefault(key string, value interface{}) {
if cfg.IsSet(key) {
return
}
cfg[key] = value
}
//--------------------------------------------------------------------------------
// A little convenient hack to notify listeners upon config changes.
type Configurable func(Config)
var mtx sync.Mutex
var globalConfig Config
var confs []Configurable
func OnConfig(conf func(Config)) {
mtx.Lock()
defer mtx.Unlock()
confs = append(confs, conf)
if globalConfig != nil {
conf(globalConfig)
}
}
func ApplyConfig(config Config) {
mtx.Lock()
globalConfig = config
confsCopy := make([]Configurable, len(confs))
copy(confsCopy, confs)
mtx.Unlock()
for _, conf := range confsCopy {
conf(config)
}
}