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 (
|
2017-02-18 03:30:54 +00:00
|
|
|
"fmt"
|
2017-01-04 17:50:49 +00:00
|
|
|
"os"
|
2020-01-02 14:00:55 +00:00
|
|
|
"sync"
|
2017-02-12 05:14:50 +00:00
|
|
|
|
|
|
|
"github.com/bcicen/ctop/logging"
|
2017-01-04 17:50:49 +00:00
|
|
|
)
|
|
|
|
|
2017-02-09 03:49:46 +00:00
|
|
|
var (
|
2017-02-16 03:49:41 +00:00
|
|
|
GlobalParams []*Param
|
|
|
|
GlobalSwitches []*Switch
|
2020-01-02 19:28:51 +00:00
|
|
|
GlobalColumns []*Column
|
2020-01-02 14:00:55 +00:00
|
|
|
lock sync.RWMutex
|
2017-02-16 03:49:41 +00:00
|
|
|
log = logging.Init()
|
2017-02-09 03:49:46 +00:00
|
|
|
)
|
2017-01-06 12:59:45 +00:00
|
|
|
|
2017-02-16 03:49:41 +00:00
|
|
|
func Init() {
|
2020-01-02 14:00:55 +00:00
|
|
|
for _, p := range defaultParams {
|
2017-02-16 03:49:41 +00:00
|
|
|
GlobalParams = append(GlobalParams, p)
|
2020-01-02 19:28:51 +00:00
|
|
|
log.Infof("loaded default config param [%s]: %s", quote(p.Key), quote(p.Val))
|
2017-01-03 17:37:09 +00:00
|
|
|
}
|
2020-01-02 14:00:55 +00:00
|
|
|
for _, s := range defaultSwitches {
|
2017-02-16 03:49:41 +00:00
|
|
|
GlobalSwitches = append(GlobalSwitches, s)
|
2020-01-02 19:28:51 +00:00
|
|
|
log.Infof("loaded default config switch [%s]: %t", quote(s.Key), s.Val)
|
2020-01-02 14:00:55 +00:00
|
|
|
}
|
2020-01-02 19:28:51 +00:00
|
|
|
for _, c := range defaultColumns {
|
|
|
|
x := c
|
|
|
|
GlobalColumns = append(GlobalColumns, &x)
|
|
|
|
log.Infof("loaded default widget config [%s]: %t", quote(x.Name), x.Enabled)
|
2017-02-12 06:03:10 +00:00
|
|
|
}
|
2017-01-03 17:37:09 +00:00
|
|
|
}
|
2017-02-16 03:02:13 +00:00
|
|
|
|
2017-02-18 03:30:54 +00:00
|
|
|
func quote(s string) string {
|
|
|
|
return fmt.Sprintf("\"%s\"", s)
|
|
|
|
}
|
|
|
|
|
2017-02-16 03:02:13 +00:00
|
|
|
// 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
|
|
|
|
}
|