export config struct fields, add convenience methods for value fetching

This commit is contained in:
Bradley Cicenas 2017-02-16 04:06:05 +00:00
parent 5bbce31601
commit a1f860a020
8 changed files with 67 additions and 65 deletions

View File

@ -15,12 +15,12 @@ var (
func Init() { func Init() {
for _, p := range params { for _, p := range params {
GlobalParams = append(GlobalParams, 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 _, s := range switches { for _, s := range switches {
GlobalSwitches = append(GlobalSwitches, s) 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)
} }
} }

View File

@ -3,46 +3,47 @@ package config
// defaults // defaults
var params = []*Param{ var params = []*Param{
&Param{ &Param{
key: "dockerHost", Key: "dockerHost",
val: getEnv("DOCKER_HOST", "unix:///var/run/docker.sock"), Val: getEnv("DOCKER_HOST", "unix:///var/run/docker.sock"),
label: "Docker API URL", Label: "Docker API URL",
}, },
&Param{ &Param{
key: "filterStr", Key: "filterStr",
val: "", Val: "",
label: "Container Name or ID Filter", Label: "Container Name or ID Filter",
}, },
&Param{ &Param{
key: "sortField", Key: "sortField",
val: "id", Val: "id",
label: "Container Sort Field", Label: "Container Sort Field",
}, },
} }
type Param struct { type Param struct {
key string Key string
val string Val string
label string Label string
} }
// Return param value // Get Param by key
func Get(k string) string { func Get(k string) *Param {
for _, p := range GlobalParams { for _, p := range GlobalParams {
if p.key == k { if p.Key == k {
return p.val return p
} }
} }
return "" // default return &Param{} // default
}
// Get Param value by key
func GetVal(k string) string {
return Get(k).Val
} }
// Set param value // Set param value
func Update(k, v string) { func Update(k, v string) {
for _, p := range GlobalParams { p := Get(k)
if p.key == k { log.Noticef("config change: %s: %s -> %s", k, p.Val, v)
log.Noticef("config change: %s: %s -> %s", k, p.val, v) p.Val = v
p.val = v // log.Errorf("ignoring update for non-existant parameter: %s", k)
return
}
}
log.Errorf("ignoring update for non-existant parameter: %s", k)
} }

View File

@ -3,52 +3,53 @@ package config
// defaults // defaults
var switches = []*Switch{ var switches = []*Switch{
&Switch{ &Switch{
key: "sortReversed", Key: "sortReversed",
val: false, Val: false,
label: "Reverse Sort Order", Label: "Reverse Sort Order",
}, },
&Switch{ &Switch{
key: "allContainers", Key: "allContainers",
val: false, Val: false,
label: "Show All Containers", Label: "Show All Containers",
}, },
&Switch{ &Switch{
key: "enableHeader", Key: "enableHeader",
val: false, Val: false,
label: "Enable cTop Status Line", Label: "Enable cTop Status Line",
}, },
&Switch{ &Switch{
key: "loggingEnabled", Key: "loggingEnabled",
val: true, Val: true,
label: "Enable Logging Server", Label: "Enable Logging Server",
}, },
} }
type Switch struct { type Switch struct {
key string Key string
val bool Val bool
label string Label string
} }
// Return toggle value // Return Switch by key
func GetSwitch(k string) bool { func GetSwitch(k string) *Switch {
for _, sw := range GlobalSwitches { for _, sw := range GlobalSwitches {
if sw.key == k { if sw.Key == k {
return sw.val 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 // Toggle a boolean switch
func Toggle(k string) { func Toggle(k string) {
for _, sw := range GlobalSwitches { sw := GetSwitch(k)
if sw.key == k { newVal := sw.Val != true
newVal := sw.val != true log.Noticef("config change: %s: %t -> %t", k, sw.Val, newVal)
log.Noticef("config change: %s: %t -> %t", k, sw.val, newVal) sw.Val = newVal
sw.val = newVal //log.Errorf("ignoring toggle for non-existant switch: %s", k)
return
}
}
log.Errorf("ignoring toggle for non-existant switch: %s", k)
} }

View File

@ -14,7 +14,7 @@ import (
func NewContainerMap() *ContainerMap { func NewContainerMap() *ContainerMap {
// init docker client // init docker client
client, err := docker.NewClient(config.Get("dockerHost")) client, err := docker.NewClient(config.GetVal("dockerHost"))
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -100,7 +100,7 @@ func (cm *ContainerMap) Del(ids ...string) {
func (cm *ContainerMap) All() []*Container { func (cm *ContainerMap) All() []*Container {
var containers Containers var containers Containers
filter := config.Get("filterStr") filter := config.GetVal("filterStr")
re := regexp.MustCompile(fmt.Sprintf(".*%s", filter)) re := regexp.MustCompile(fmt.Sprintf(".*%s", filter))
for _, c := range cm.containers { for _, c := range cm.containers {

View File

@ -75,14 +75,14 @@ func (g *Grid) redrawRows() {
ui.Clear() ui.Clear()
// build layout // build layout
if config.GetSwitch("enableHeader") { if config.GetSwitchVal("enableHeader") {
g.header.SetCount(len(g.containers)) 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(g.header.Row())
} }
ui.Body.AddRows(fieldHeader()) ui.Body.AddRows(fieldHeader())
for _, c := range g.containers { for _, c := range g.containers {
if !config.GetSwitch("allContainers") && c.state != "running" { if !config.GetSwitchVal("allContainers") && c.state != "running" {
continue continue
} }
ui.Body.AddRows(c.widgets.Row()) ui.Body.AddRows(c.widgets.Row())

View File

@ -11,7 +11,7 @@ var log *logging.CTopLogger
func main() { func main() {
config.Init() config.Init()
log = logging.Init() log = logging.Init()
if config.GetSwitch("loggingEnabled") { if config.GetSwitchVal("loggingEnabled") {
log.StartServer() log.StartServer()
} }
if err := ui.Init(); err != nil { if err := ui.Init(); err != nil {

View File

@ -64,7 +64,7 @@ func SortMenu() {
} }
// set cursor position to current sort field // set cursor position to current sort field
//current := config.Get("sortField") //current := config.GetVal("sortField")
//for n, item := range m.Items { //for n, item := range m.Items {
//if item.Val == current { //if item.Val == current {
//m.CursorPos = n //m.CursorPos = n

View File

@ -27,8 +27,8 @@ type Containers []*Container
func (a Containers) Len() int { return len(a) } 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) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Containers) Less(i, j int) bool { func (a Containers) Less(i, j int) bool {
f := Sorters[config.Get("sortField")] f := Sorters[config.GetVal("sortField")]
if config.GetSwitch("sortReversed") { if config.GetSwitchVal("sortReversed") {
return f(a[j], a[i]) return f(a[j], a[i])
} }
return f(a[i], a[j]) return f(a[i], a[j])