From 90f6ce396272b3c8850cc33820e7e9d41c0324e6 Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Thu, 16 Feb 2017 03:02:13 +0000 Subject: [PATCH] refactor config, add Switch and Param struct + config labels --- config/main.go | 65 +++++++++++++++++++----------------------------- config/param.go | 33 ++++++++++++++++++++++++ config/toggle.go | 44 ++++++++++++++++++++++++++++++++ grid.go | 4 +-- main.go | 2 +- sort.go | 2 +- 6 files changed, 106 insertions(+), 44 deletions(-) create mode 100644 config/param.go create mode 100644 config/toggle.go diff --git a/config/main.go b/config/main.go index 1f8b082..6a68a7d 100644 --- a/config/main.go +++ b/config/main.go @@ -12,31 +12,9 @@ var ( ) type Config struct { - params map[string]string - toggles map[string]bool - updates chan ConfigMsg -} - -// Return param value -func Get(k string) string { - if _, ok := Global.params[k]; ok == true { - return Global.params[k] - } - return "" -} - -// Return toggle value -func GetToggle(k string) bool { - if _, ok := Global.toggles[k]; ok == true { - return Global.toggles[k] - } - return false -} - -// Toggle a boolean option -func Toggle(k string) { - Global.toggles[k] = Global.toggles[k] != true - log.Noticef("config change: %s = %t", k, Global.toggles[k]) + params map[string]*Param + switches map[string]*Switch + updates chan ConfigMsg } type ConfigMsg struct { @@ -49,30 +27,37 @@ func Update(k, v string) { } func NewDefaultConfig() Config { - docker := os.Getenv("DOCKER_HOST") - if docker == "" { - docker = "unix:///var/run/docker.sock" + config := Config{ + params: make(map[string]*Param), + switches: make(map[string]*Switch), + updates: make(chan ConfigMsg), } - params := map[string]string{ - "dockerHost": docker, - "filterStr": "", - "sortField": "id", + for _, p := range params { + config.params[p.key] = p + log.Debugf("loaded config param: \"%s\": \"%s\"", p.key, p.val) } - toggles := map[string]bool{ - "sortReverse": false, - "allContainers": false, - "enableHeader": false, - "loggingEnabled": true, + for _, t := range switches { + config.switches[t.key] = t + log.Debugf("loaded config switch: \"%s\": %t", t.key, t.val) } - config := Config{params, toggles, make(chan ConfigMsg)} go func() { for m := range config.updates { - config.params[m.key] = m.val - log.Noticef("config change: %s = %s", m.key, m.val) + 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 +func getEnv(key, defaultVal string) string { + val := os.Getenv(key) + if val != "" { + return val + } + return defaultVal +} diff --git a/config/param.go b/config/param.go new file mode 100644 index 0000000..7e3b7f5 --- /dev/null +++ b/config/param.go @@ -0,0 +1,33 @@ +package config + +var params = []*Param{ + &Param{ + key: "dockerHost", + val: getEnv("DOCKER_HOST", "unix:///var/run/docker.sock"), + label: "Docker API URL", + }, + &Param{ + key: "filterStr", + val: "", + label: "Container Name or ID Filter", + }, + &Param{ + key: "sortField", + val: "id", + label: "Container Sort Field", + }, +} + +type Param struct { + key string + val string + label string +} + +// Return param value +func Get(k string) string { + if _, ok := Global.params[k]; ok == true { + return Global.params[k].val + } + return "" +} diff --git a/config/toggle.go b/config/toggle.go new file mode 100644 index 0000000..f6f95b0 --- /dev/null +++ b/config/toggle.go @@ -0,0 +1,44 @@ +package config + +var switches = []*Switch{ + &Switch{ + key: "sortReverse", + val: false, + label: "Reverse Sort Order", + }, + &Switch{ + key: "allContainers", + val: false, + label: "Show All Containers", + }, + &Switch{ + key: "enableHeader", + val: false, + label: "Enable cTop Status Line", + }, + &Switch{ + key: "loggingEnabled", + val: true, + label: "Enable Logging Server", + }, +} + +type Switch struct { + key string + val bool + label string +} + +// Return toggle value +func GetSwitch(k string) bool { + if _, ok := Global.switches[k]; ok == true { + return Global.switches[k].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) +} diff --git a/grid.go b/grid.go index 98cd657..ee750f5 100644 --- a/grid.go +++ b/grid.go @@ -75,14 +75,14 @@ func (g *Grid) redrawRows() { ui.Clear() // build layout - if config.GetToggle("enableHeader") { + if config.GetSwitch("enableHeader") { g.header.SetCount(len(g.containers)) g.header.SetFilter(config.Get("filterStr")) ui.Body.AddRows(g.header.Row()) } ui.Body.AddRows(fieldHeader()) for _, c := range g.containers { - if !config.GetToggle("allContainers") && c.state != "running" { + if !config.GetSwitch("allContainers") && c.state != "running" { continue } ui.Body.AddRows(c.widgets.Row()) diff --git a/main.go b/main.go index a23a69d..3ca66f0 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ var log *logging.CTopLogger func main() { log = logging.Init() - if config.GetToggle("loggingEnabled") { + if config.GetSwitch("loggingEnabled") { log.StartServer() } if err := ui.Init(); err != nil { diff --git a/sort.go b/sort.go index f391f76..f1f8dd5 100644 --- a/sort.go +++ b/sort.go @@ -28,7 +28,7 @@ func (a Containers) Len() int { return len(a) } func (a Containers) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a Containers) Less(i, j int) bool { f := Sorters[config.Get("sortField")] - if config.GetToggle("sortReversed") { + if config.GetSwitch("sortReversed") { return f(a[j], a[i]) } return f(a[i], a[j])