ctop/config/main.go

77 lines
1.3 KiB
Go
Raw Normal View History

2017-02-07 03:33:09 +00:00
package config
2017-01-03 17:37:09 +00:00
2017-01-04 17:50:49 +00:00
import (
"os"
"github.com/bcicen/ctop/logging"
2017-01-04 17:50:49 +00:00
)
2017-02-09 03:49:46 +00:00
var (
Global = NewDefaultConfig()
log = logging.Init()
2017-02-09 03:49:46 +00:00
)
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
}
type ConfigMsg struct {
key string
val string
2017-01-03 17:37:09 +00:00
}
2017-02-07 03:33:09 +00:00
func Update(k, v string) {
log.Noticef("config update: %s = %s", k, v)
Global.updates <- ConfigMsg{k, v}
2017-01-12 19:48:29 +00:00
}
2017-01-03 17:37:09 +00:00
func NewDefaultConfig() Config {
2017-01-04 17:50:49 +00:00
docker := os.Getenv("DOCKER_HOST")
if docker == "" {
docker = "unix:///var/run/docker.sock"
}
params := map[string]string{
"dockerHost": docker,
"filterStr": "",
"sortField": "id",
2017-01-03 17:37:09 +00:00
}
toggles := map[string]bool{
"sortReverse": false,
"enableHeader": false,
"loggingEnabled": true,
}
config := Config{params, toggles, make(chan ConfigMsg)}
go func() {
for m := range config.updates {
config.params[m.key] = m.val
}
}()
return config
2017-01-03 17:37:09 +00:00
}