mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
refactor config, add Switch and Param struct + config labels
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
33
config/param.go
Normal file
33
config/param.go
Normal file
@@ -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 ""
|
||||
}
|
||||
44
config/toggle.go
Normal file
44
config/toggle.go
Normal file
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user