sampler-fork/config/config.go

65 lines
1.3 KiB
Go
Raw Normal View History

2019-01-25 04:10:38 +00:00
package config
import (
2019-02-02 14:45:53 +00:00
"github.com/sqshq/sampler/console"
"github.com/sqshq/sampler/data"
. "github.com/sqshq/sampler/widgets"
2019-01-25 04:10:38 +00:00
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
2019-01-25 04:10:38 +00:00
)
type Config struct {
Theme console.Theme `yaml:"theme"`
RunCharts []RunChartConfig `yaml:"runcharts"`
2019-01-31 00:02:38 +00:00
}
2019-02-01 05:07:25 +00:00
type RunChartConfig struct {
2019-02-15 04:34:45 +00:00
Title string `yaml:"title"`
Items []data.Item `yaml:"items"`
Position Position `yaml:"position"`
Size Size `yaml:"size"`
RefreshRateMs int `yaml:"refresh-rate-ms"`
Precision int `yaml:"decimal-places"`
Legend LegendConfig `yaml:"legend"`
2019-01-25 04:10:38 +00:00
}
2019-02-15 04:34:45 +00:00
type LegendConfig struct {
Enabled bool `yaml:"enabled"`
Details bool `yaml:"details"`
}
func Load() *Config {
2019-01-25 04:10:38 +00:00
2019-02-15 04:34:45 +00:00
if len(os.Args) < 2 {
println("Please specify config file location. See www.github.com/sqshq/sampler for the reference")
os.Exit(0)
}
2019-02-15 04:34:45 +00:00
cfg := readFile(os.Args[1])
2019-02-01 00:02:34 +00:00
cfg.validate()
cfg.setDefaultValues()
cfg.setDefaultColors()
cfg.setDefaultLayout()
2019-01-31 23:40:05 +00:00
return cfg
}
func readFile(location string) *Config {
2019-01-25 04:10:38 +00:00
yamlFile, err := ioutil.ReadFile(location)
if err != nil {
log.Fatalf("Can't read config file: %s", location)
}
2019-01-28 23:09:52 +00:00
cfg := new(Config)
2019-01-25 04:10:38 +00:00
err = yaml.Unmarshal(yamlFile, cfg)
if err != nil {
log.Fatalf("Can't read config file: %v", err)
}
return cfg
}