2017-02-07 03:33:09 +00:00
|
|
|
package config
|
2017-01-03 17:37:09 +00:00
|
|
|
|
2017-01-04 17:50:49 +00:00
|
|
|
import (
|
|
|
|
"os"
|
2017-02-12 05:14:50 +00:00
|
|
|
|
|
|
|
"github.com/bcicen/ctop/logging"
|
2017-01-04 17:50:49 +00:00
|
|
|
)
|
|
|
|
|
2017-02-09 03:49:46 +00:00
|
|
|
var (
|
2017-02-12 06:03:10 +00:00
|
|
|
Global = NewDefaultConfig()
|
|
|
|
log = logging.Init()
|
2017-02-09 03:49:46 +00:00
|
|
|
)
|
2017-01-06 12:59:45 +00:00
|
|
|
|
2017-02-12 06:03:10 +00:00
|
|
|
type Config struct {
|
2017-02-16 03:02:13 +00:00
|
|
|
params map[string]*Param
|
|
|
|
switches map[string]*Switch
|
|
|
|
updates chan ConfigMsg
|
2017-02-12 06:03:10 +00:00
|
|
|
}
|
2017-01-06 12:59:45 +00:00
|
|
|
|
|
|
|
type ConfigMsg struct {
|
|
|
|
key string
|
|
|
|
val string
|
2017-01-03 17:37:09 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 03:33:09 +00:00
|
|
|
func Update(k, v string) {
|
2017-02-12 06:03:10 +00:00
|
|
|
Global.updates <- ConfigMsg{k, v}
|
2017-01-12 19:48:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 17:37:09 +00:00
|
|
|
func NewDefaultConfig() Config {
|
2017-02-16 03:02:13 +00:00
|
|
|
config := Config{
|
|
|
|
params: make(map[string]*Param),
|
|
|
|
switches: make(map[string]*Switch),
|
|
|
|
updates: make(chan ConfigMsg),
|
2017-01-04 17:50:49 +00:00
|
|
|
}
|
2017-02-12 06:03:10 +00:00
|
|
|
|
2017-02-16 03:02:13 +00:00
|
|
|
for _, p := range params {
|
|
|
|
config.params[p.key] = p
|
|
|
|
log.Debugf("loaded config param: \"%s\": \"%s\"", p.key, p.val)
|
2017-01-03 17:37:09 +00:00
|
|
|
}
|
2017-02-12 06:03:10 +00:00
|
|
|
|
2017-02-16 03:02:13 +00:00
|
|
|
for _, t := range switches {
|
|
|
|
config.switches[t.key] = t
|
|
|
|
log.Debugf("loaded config switch: \"%s\": %t", t.key, t.val)
|
2017-02-12 06:03:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 12:59:45 +00:00
|
|
|
go func() {
|
2017-02-12 06:03:10 +00:00
|
|
|
for m := range config.updates {
|
2017-02-16 03:02:13 +00:00
|
|
|
config.params[m.key].val = m.val
|
|
|
|
log.Noticef("config change: %s: %s", m.key, m.val)
|
2017-01-06 12:59:45 +00:00
|
|
|
}
|
|
|
|
}()
|
2017-02-16 03:02:13 +00:00
|
|
|
|
2017-01-06 12:59:45 +00:00
|
|
|
return config
|
2017-01-03 17:37:09 +00:00
|
|
|
}
|
2017-02-16 03:02:13 +00:00
|
|
|
|
|
|
|
// Return env var value if set, else return defaultVal
|
|
|
|
func getEnv(key, defaultVal string) string {
|
|
|
|
val := os.Getenv(key)
|
|
|
|
if val != "" {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
return defaultVal
|
|
|
|
}
|