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() {
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)
}
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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 {

View File

@ -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())

View File

@ -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 {

View File

@ -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

View File

@ -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])