diff --git a/config/main.go b/config/main.go index 6a68a7d..8b20bbb 100644 --- a/config/main.go +++ b/config/main.go @@ -7,50 +7,21 @@ import ( ) var ( - Global = NewDefaultConfig() - log = logging.Init() + GlobalParams []*Param + GlobalSwitches []*Switch + log = logging.Init() ) -type Config struct { - 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), - } - +func Init() { for _, p := range params { - config.params[p.key] = p + GlobalParams = append(GlobalParams, p) log.Debugf("loaded config param: \"%s\": \"%s\"", p.key, p.val) } - for _, t := range switches { - config.switches[t.key] = t - log.Debugf("loaded config switch: \"%s\": %t", t.key, t.val) + for _, s := range switches { + GlobalSwitches = append(GlobalSwitches, s) + 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 diff --git a/config/param.go b/config/param.go index 7e3b7f5..43e2f41 100644 --- a/config/param.go +++ b/config/param.go @@ -1,5 +1,6 @@ package config +// defaults var params = []*Param{ &Param{ key: "dockerHost", @@ -26,8 +27,22 @@ type Param struct { // Return param value func Get(k string) string { - if _, ok := Global.params[k]; ok == true { - return Global.params[k].val + for _, p := range GlobalParams { + 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) } diff --git a/config/toggle.go b/config/toggle.go index f6f95b0..20a4a6f 100644 --- a/config/toggle.go +++ b/config/toggle.go @@ -1,8 +1,9 @@ package config +// defaults var switches = []*Switch{ &Switch{ - key: "sortReverse", + key: "sortReversed", val: false, label: "Reverse Sort Order", }, @@ -31,14 +32,23 @@ type Switch struct { // Return toggle value func GetSwitch(k string) bool { - if _, ok := Global.switches[k]; ok == true { - return Global.switches[k].val + for _, sw := range GlobalSwitches { + if sw.key == k { + return sw.val + } } return false // default } // Toggle a boolean switch func Toggle(k string) { - Global.switches[k].val = Global.switches[k].val != true - log.Noticef("config change: %s: %t", k, Global.switches[k].val) + for _, sw := range GlobalSwitches { + 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) } diff --git a/main.go b/main.go index 3ca66f0..d02cc71 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( var log *logging.CTopLogger func main() { + config.Init() log = logging.Init() if config.GetSwitch("loggingEnabled") { log.StartServer()