diff --git a/config/main.go b/config/main.go index 8b20bbb..968d3bf 100644 --- a/config/main.go +++ b/config/main.go @@ -15,12 +15,12 @@ var ( func Init() { for _, p := range params { 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 _, s := range switches { GlobalSwitches = append(GlobalSwitches, s) - log.Debugf("loaded config switch: \"%s\": %t", s.key, s.val) + log.Debugf("loaded config switch: \"%s\": %t", s.Key, s.Val) } } diff --git a/config/param.go b/config/param.go index 43e2f41..a1d0584 100644 --- a/config/param.go +++ b/config/param.go @@ -3,46 +3,47 @@ package config // defaults var params = []*Param{ &Param{ - key: "dockerHost", - val: getEnv("DOCKER_HOST", "unix:///var/run/docker.sock"), - label: "Docker API URL", + 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", + Key: "filterStr", + Val: "", + Label: "Container Name or ID Filter", }, &Param{ - key: "sortField", - val: "id", - label: "Container Sort Field", + Key: "sortField", + Val: "id", + Label: "Container Sort Field", }, } type Param struct { - key string - val string - label string + Key string + Val string + Label string } -// Return param value -func Get(k string) string { +// Get Param by key +func Get(k string) *Param { for _, p := range GlobalParams { - if p.key == k { - return p.val + if p.Key == k { + return p } } - return "" // default + return &Param{} // default +} + +// Get Param value by key +func GetVal(k string) string { + return Get(k).Val } // 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) + p := Get(k) + log.Noticef("config change: %s: %s -> %s", k, p.Val, v) + p.Val = v + // log.Errorf("ignoring update for non-existant parameter: %s", k) } diff --git a/config/toggle.go b/config/toggle.go index 20a4a6f..3e1111b 100644 --- a/config/toggle.go +++ b/config/toggle.go @@ -3,52 +3,53 @@ package config // defaults var switches = []*Switch{ &Switch{ - key: "sortReversed", - val: false, - label: "Reverse Sort Order", + Key: "sortReversed", + Val: false, + Label: "Reverse Sort Order", }, &Switch{ - key: "allContainers", - val: false, - label: "Show All Containers", + Key: "allContainers", + Val: false, + Label: "Show All Containers", }, &Switch{ - key: "enableHeader", - val: false, - label: "Enable cTop Status Line", + Key: "enableHeader", + Val: false, + Label: "Enable cTop Status Line", }, &Switch{ - key: "loggingEnabled", - val: true, - label: "Enable Logging Server", + Key: "loggingEnabled", + Val: true, + Label: "Enable Logging Server", }, } type Switch struct { - key string - val bool - label string + Key string + Val bool + Label string } -// Return toggle value -func GetSwitch(k string) bool { +// Return Switch by key +func GetSwitch(k string) *Switch { for _, sw := range GlobalSwitches { - if sw.key == k { - return sw.val + if sw.Key == k { + return sw } } - return false // default + return &Switch{} // default +} + +// Return Switch value by key +func GetSwitchVal(k string) bool { + return GetSwitch(k).Val } // Toggle a boolean switch func Toggle(k string) { - 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) + sw := GetSwitch(k) + newVal := sw.Val != true + log.Noticef("config change: %s: %t -> %t", k, sw.Val, newVal) + sw.Val = newVal + //log.Errorf("ignoring toggle for non-existant switch: %s", k) } diff --git a/containermap.go b/containermap.go index 9cd767b..67f5ad2 100644 --- a/containermap.go +++ b/containermap.go @@ -14,7 +14,7 @@ import ( func NewContainerMap() *ContainerMap { // init docker client - client, err := docker.NewClient(config.Get("dockerHost")) + client, err := docker.NewClient(config.GetVal("dockerHost")) if err != nil { panic(err) } @@ -100,7 +100,7 @@ func (cm *ContainerMap) Del(ids ...string) { func (cm *ContainerMap) All() []*Container { var containers Containers - filter := config.Get("filterStr") + filter := config.GetVal("filterStr") re := regexp.MustCompile(fmt.Sprintf(".*%s", filter)) for _, c := range cm.containers { diff --git a/grid.go b/grid.go index ee750f5..cba968c 100644 --- a/grid.go +++ b/grid.go @@ -75,14 +75,14 @@ func (g *Grid) redrawRows() { ui.Clear() // build layout - if config.GetSwitch("enableHeader") { + if config.GetSwitchVal("enableHeader") { g.header.SetCount(len(g.containers)) - g.header.SetFilter(config.Get("filterStr")) + g.header.SetFilter(config.GetVal("filterStr")) ui.Body.AddRows(g.header.Row()) } ui.Body.AddRows(fieldHeader()) for _, c := range g.containers { - if !config.GetSwitch("allContainers") && c.state != "running" { + if !config.GetSwitchVal("allContainers") && c.state != "running" { continue } ui.Body.AddRows(c.widgets.Row()) diff --git a/main.go b/main.go index d02cc71..784d522 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ var log *logging.CTopLogger func main() { config.Init() log = logging.Init() - if config.GetSwitch("loggingEnabled") { + if config.GetSwitchVal("loggingEnabled") { log.StartServer() } if err := ui.Init(); err != nil { diff --git a/menus.go b/menus.go index 8ba324f..8e33a15 100644 --- a/menus.go +++ b/menus.go @@ -64,7 +64,7 @@ func SortMenu() { } // set cursor position to current sort field - //current := config.Get("sortField") + //current := config.GetVal("sortField") //for n, item := range m.Items { //if item.Val == current { //m.CursorPos = n diff --git a/sort.go b/sort.go index f1f8dd5..3e0d86b 100644 --- a/sort.go +++ b/sort.go @@ -27,8 +27,8 @@ type Containers []*Container 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.GetSwitch("sortReversed") { + f := Sorters[config.GetVal("sortField")] + if config.GetSwitchVal("sortReversed") { return f(a[j], a[i]) } return f(a[i], a[j])