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

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