sampler-fork/config/config.go

74 lines
1.4 KiB
Go
Raw Normal View History

2019-01-25 04:10:38 +00:00
package config
import (
2019-01-31 23:40:05 +00:00
"github.com/sqshq/vcmd/data"
2019-01-31 00:02:38 +00:00
. "github.com/sqshq/vcmd/layout"
2019-01-31 23:40:05 +00:00
"github.com/sqshq/vcmd/settings"
2019-01-25 04:10:38 +00:00
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
)
type Config struct {
2019-01-31 23:40:05 +00:00
Theme settings.Theme `yaml:"theme"`
RunCharts []RunChart `yaml:"run-charts"`
2019-01-31 00:02:38 +00:00
}
2019-01-31 01:41:51 +00:00
type RunChart struct {
2019-01-31 23:40:05 +00:00
Title string `yaml:"title"`
Items []data.Item `yaml:"data"`
Position Position `yaml:"position"`
Size Size `yaml:"size"`
RefreshRateMs int `yaml:"refresh-rate-ms"`
TimeScaleSec int `yaml:"time-scale-sec"`
2019-01-25 04:10:38 +00:00
}
func Load(location string) *Config {
2019-01-31 23:40:05 +00:00
cfg := readFile(location)
validate(cfg)
setColors(cfg)
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
}
2019-01-31 23:40:05 +00:00
/*
TODO validation
- title uniquness and mandatory within a single type of widget
- label uniqueness and mandatory (if > 1 data bullets)
*/
func validate(config *Config) {
}
func setColors(config *Config) {
palette := settings.GetPalette(config.Theme)
for i, chart := range config.RunCharts {
for j, item := range chart.Items {
if item.Color == 0 {
item.Color = palette.Colors[i+j]
chart.Items[j] = item
}
}
}
}