simplify config, export GlobalParams+GlobalSwitches

This commit is contained in:
Bradley Cicenas 2017-02-16 03:49:41 +00:00
parent 90f6ce3962
commit 5bbce31601
4 changed files with 42 additions and 45 deletions

View File

@ -7,50 +7,21 @@ import (
) )
var ( var (
Global = NewDefaultConfig() GlobalParams []*Param
log = logging.Init() GlobalSwitches []*Switch
log = logging.Init()
) )
type Config struct { func Init() {
params map[string]*Param
switches map[string]*Switch
updates chan ConfigMsg
}
type ConfigMsg struct {
key string
val string
}
func Update(k, v string) {
Global.updates <- ConfigMsg{k, v}
}
func NewDefaultConfig() Config {
config := Config{
params: make(map[string]*Param),
switches: make(map[string]*Switch),
updates: make(chan ConfigMsg),
}
for _, p := range params { for _, p := range params {
config.params[p.key] = p GlobalParams = append(GlobalParams, p)
log.Debugf("loaded config param: \"%s\": \"%s\"", p.key, p.val) log.Debugf("loaded config param: \"%s\": \"%s\"", p.key, p.val)
} }
for _, t := range switches { for _, s := range switches {
config.switches[t.key] = t GlobalSwitches = append(GlobalSwitches, s)
log.Debugf("loaded config switch: \"%s\": %t", t.key, t.val) log.Debugf("loaded config switch: \"%s\": %t", s.key, s.val)
} }
go func() {
for m := range config.updates {
config.params[m.key].val = m.val
log.Noticef("config change: %s: %s", m.key, m.val)
}
}()
return config
} }
// Return env var value if set, else return defaultVal // Return env var value if set, else return defaultVal

View File

@ -1,5 +1,6 @@
package config package config
// defaults
var params = []*Param{ var params = []*Param{
&Param{ &Param{
key: "dockerHost", key: "dockerHost",
@ -26,8 +27,22 @@ type Param struct {
// Return param value // Return param value
func Get(k string) string { func Get(k string) string {
if _, ok := Global.params[k]; ok == true { for _, p := range GlobalParams {
return Global.params[k].val if p.key == k {
return p.val
}
} }
return "" return "" // default
}
// Set param value
func Update(k, v string) {
for _, p := range GlobalParams {
if p.key == k {
log.Noticef("config change: %s: %s -> %s", k, p.val, v)
p.val = v
return
}
}
log.Errorf("ignoring update for non-existant parameter: %s", k)
} }

View File

@ -1,8 +1,9 @@
package config package config
// defaults
var switches = []*Switch{ var switches = []*Switch{
&Switch{ &Switch{
key: "sortReverse", key: "sortReversed",
val: false, val: false,
label: "Reverse Sort Order", label: "Reverse Sort Order",
}, },
@ -31,14 +32,23 @@ type Switch struct {
// Return toggle value // Return toggle value
func GetSwitch(k string) bool { func GetSwitch(k string) bool {
if _, ok := Global.switches[k]; ok == true { for _, sw := range GlobalSwitches {
return Global.switches[k].val if sw.key == k {
return sw.val
}
} }
return false // default return false // default
} }
// Toggle a boolean switch // Toggle a boolean switch
func Toggle(k string) { func Toggle(k string) {
Global.switches[k].val = Global.switches[k].val != true for _, sw := range GlobalSwitches {
log.Noticef("config change: %s: %t", k, Global.switches[k].val) if sw.key == k {
newVal := sw.val != true
log.Noticef("config change: %s: %t -> %t", k, sw.val, newVal)
sw.val = newVal
return
}
}
log.Errorf("ignoring toggle for non-existant switch: %s", k)
} }

View File

@ -9,6 +9,7 @@ import (
var log *logging.CTopLogger var log *logging.CTopLogger
func main() { func main() {
config.Init()
log = logging.Init() log = logging.Init()
if config.GetSwitch("loggingEnabled") { if config.GetSwitch("loggingEnabled") {
log.StartServer() log.StartServer()