sampler-fork/config/config.go

115 lines
2.6 KiB
Go
Raw Normal View History

2019-01-25 04:10:38 +00:00
package config
import (
"fmt"
"github.com/jessevdk/go-flags"
2019-02-02 14:45:53 +00:00
"github.com/sqshq/sampler/console"
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,omitempty"`
2019-04-09 02:04:08 +00:00
Variables map[string]string `yaml:"variables,omitempty"`
RunCharts []RunChartConfig `yaml:"runcharts,omitempty"`
BarCharts []BarChartConfig `yaml:"barcharts,omitempty"`
Gauges []GaugeConfig `yaml:"gauges,omitempty"`
AsciiBoxes []AsciiBoxConfig `yaml:"asciiboxes,omitempty"`
SparkLines []SparkLineConfig `yaml:"sparklines,omitempty"`
}
func Load() (Config, Options) {
2019-02-25 00:08:36 +00:00
var opt Options
_, err := flags.Parse(&opt)
2019-01-25 04:10:38 +00:00
if err != nil {
2019-03-17 00:54:24 +00:00
os.Exit(0)
}
cfg := readFile(opt.ConfigFile)
2019-02-01 00:02:34 +00:00
cfg.validate()
cfg.setDefaults()
2019-01-31 23:40:05 +00:00
return *cfg, opt
2019-01-31 23:40:05 +00:00
}
2019-03-20 02:12:45 +00:00
func Update(settings []ComponentSettings, options Options) {
cfg := readFile(options.ConfigFile)
for _, s := range settings {
componentConfig := cfg.findComponent(s.Type, s.Title)
componentConfig.Position = getPosition(s.Location, s.Size)
}
2019-03-20 02:12:45 +00:00
saveFile(cfg, options.ConfigFile)
}
func (c *Config) findComponent(componentType ComponentType, componentTitle string) *ComponentConfig {
switch componentType {
case TypeRunChart:
for i, component := range c.RunCharts {
if component.Title == componentTitle {
return &c.RunCharts[i].ComponentConfig
}
}
2019-02-21 04:53:59 +00:00
case TypeBarChart:
for i, component := range c.BarCharts {
if component.Title == componentTitle {
return &c.BarCharts[i].ComponentConfig
}
}
2019-02-26 04:36:23 +00:00
case TypeGauge:
for i, component := range c.Gauges {
if component.Title == componentTitle {
return &c.Gauges[i].ComponentConfig
}
}
2019-02-17 23:00:00 +00:00
case TypeAsciiBox:
for i, component := range c.AsciiBoxes {
if component.Title == componentTitle {
return &c.AsciiBoxes[i].ComponentConfig
}
}
case TypeSparkLine:
for i, component := range c.SparkLines {
if component.Title == componentTitle {
return &c.SparkLines[i].ComponentConfig
}
}
}
panic(fmt.Sprintf(
"Can't find component type %v with title %v", componentType, componentTitle))
}
2019-01-31 23:40:05 +00:00
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-03-20 02:12:45 +00:00
func saveFile(config *Config, fileName string) {
file, err := yaml.Marshal(config)
if err != nil {
log.Fatalf("Can't marshal config file: %v", err)
}
2019-03-20 02:12:45 +00:00
err = ioutil.WriteFile(fileName, file, 0644)
if err != nil {
log.Fatalf("Can't save config file: %v", err)
}
}