sampler-fork/config/config.go

111 lines
2.4 KiB
Go
Raw Normal View History

2019-01-25 04:10:38 +00:00
package config
import (
"fmt"
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 {
2019-02-17 23:00:00 +00:00
Theme *console.Theme `yaml:"theme,omitempty"`
RunCharts []RunChartConfig `yaml:"runcharts,omitempty"`
2019-02-19 04:07:32 +00:00
BarCharts []BarChartConfig `yaml:"barcharts,omitempty"`
2019-02-26 04:36:23 +00:00
Gauges []GaugeConfig `yaml:"gauges,omitempty"`
2019-02-17 23:00:00 +00:00
AsciiBoxes []AsciiBoxConfig `yaml:"asciiboxes,omitempty"`
}
2019-02-25 00:08:36 +00:00
type Flags struct {
ConfigFileName string
Variables map[string]string
}
func Load() (Config, Flags) {
2019-01-25 04:10:38 +00:00
2019-03-17 00:54:24 +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-03-17 00:54:24 +00:00
cfg := readFile(os.Args[1])
2019-02-01 00:02:34 +00:00
cfg.validate()
cfg.setDefaults()
2019-01-31 23:40:05 +00:00
2019-03-17 00:54:24 +00:00
flg := Flags{ConfigFileName: os.Args[1]}
2019-02-25 00:08:36 +00:00
return *cfg, flg
2019-01-31 23:40:05 +00:00
}
func Update(settings []ComponentSettings) {
cfg := readFile(os.Args[1])
for _, s := range settings {
componentConfig := cfg.findComponent(s.Type, s.Title)
componentConfig.Position = getPosition(s.Location, s.Size)
}
saveFile(cfg)
}
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
}
}
}
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
}
func saveFile(config *Config) {
file, err := yaml.Marshal(config)
if err != nil {
log.Fatalf("Can't marshal config file: %v", err)
}
2019-02-25 00:37:57 +00:00
err = ioutil.WriteFile(os.Args[1], file, 0644)
if err != nil {
log.Fatalf("Can't save config file: %v", err)
}
}