mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
refactor config, add Switch and Param struct + config labels
This commit is contained in:
parent
5f13563b6f
commit
90f6ce3962
@ -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)
|
||||
}
|
4
grid.go
4
grid.go
@ -75,14 +75,14 @@ func (g *Grid) redrawRows() {
|
||||
ui.Clear()
|
||||
|
||||
// build layout
|
||||
if config.GetToggle("enableHeader") {
|
||||
if config.GetSwitch("enableHeader") {
|
||||
g.header.SetCount(len(g.containers))
|
||||
g.header.SetFilter(config.Get("filterStr"))
|
||||
ui.Body.AddRows(g.header.Row())
|
||||
}
|
||||
ui.Body.AddRows(fieldHeader())
|
||||
for _, c := range g.containers {
|
||||
if !config.GetToggle("allContainers") && c.state != "running" {
|
||||
if !config.GetSwitch("allContainers") && c.state != "running" {
|
||||
continue
|
||||
}
|
||||
ui.Body.AddRows(c.widgets.Row())
|
||||
|
2
main.go
2
main.go
@ -10,7 +10,7 @@ var log *logging.CTopLogger
|
||||
|
||||
func main() {
|
||||
log = logging.Init()
|
||||
if config.GetToggle("loggingEnabled") {
|
||||
if config.GetSwitch("loggingEnabled") {
|
||||
log.StartServer()
|
||||
}
|
||||
if err := ui.Init(); err != nil {
|
||||
|
2
sort.go
2
sort.go
@ -28,7 +28,7 @@ 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.GetToggle("sortReversed") {
|
||||
if config.GetSwitch("sortReversed") {
|
||||
return f(a[j], a[i])
|
||||
}
|
||||
return f(a[i], a[j])
|
||||
|
Loading…
Reference in New Issue
Block a user