sampler-fork/config/config.go

129 lines
3.0 KiB
Go
Raw Permalink 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-06-21 04:45:32 +00:00
"gopkg.in/yaml.v3"
2019-01-25 04:10:38 +00:00
"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"`
SparkLines []SparkLineConfig `yaml:"sparklines,omitempty"`
2019-04-10 02:30:21 +00:00
TextBoxes []TextBoxConfig `yaml:"textboxes,omitempty"`
AsciiBoxes []AsciiBoxConfig `yaml:"asciiboxes,omitempty"`
}
2019-05-24 02:58:46 +00:00
func LoadConfig() (*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 {
console.Exit("")
2019-05-24 02:58:46 +00:00
}
if opt.Version == true {
console.Exit(console.AppVersion)
2019-05-24 02:58:46 +00:00
}
2019-12-22 02:05:01 +00:00
if opt.ConfigFile == nil {
console.Exit("Please specify config file using --config flag. Example: sampler --config example.yml")
2019-03-17 00:54:24 +00:00
}
cfg := readFile(opt.ConfigFile)
2019-02-01 00:02:34 +00:00
cfg.validate()
cfg.setDefaults()
2019-01-31 23:40:05 +00:00
2019-05-24 02:58:46 +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-04-10 02:30:21 +00:00
case TypeSparkLine:
for i, component := range c.SparkLines {
if component.Title == componentTitle {
return &c.SparkLines[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
}
}
2019-04-10 02:30:21 +00:00
case TypeTextBox:
for i, component := range c.TextBoxes {
if component.Title == componentTitle {
2019-04-10 02:30:21 +00:00
return &c.TextBoxes[i].ComponentConfig
}
}
}
panic(fmt.Sprintf(
2019-05-20 04:14:13 +00:00
"Failed to find component type %v with title %v", componentType, componentTitle))
}
2019-05-24 02:58:46 +00:00
func readFile(location *string) *Config {
2019-01-31 23:40:05 +00:00
2019-05-24 02:58:46 +00:00
yamlFile, err := ioutil.ReadFile(*location)
2019-01-25 04:10:38 +00:00
if err != nil {
2019-05-24 02:58:46 +00:00
log.Fatalf("Failed to read config file: %s", *location)
2019-01-25 04:10:38 +00:00
}
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 {
2019-05-20 04:14:13 +00:00
log.Fatalf("Failed to read config file: %v", err)
2019-01-25 04:10:38 +00:00
}
return cfg
}
2019-05-24 02:58:46 +00:00
func saveFile(config *Config, fileName *string) {
file, err := yaml.Marshal(config)
if err != nil {
2019-05-20 04:14:13 +00:00
log.Fatalf("Failed to marshal config file: %v", err)
}
2019-05-24 02:58:46 +00:00
err = ioutil.WriteFile(*fileName, file, os.ModePerm)
if err != nil {
2019-05-20 04:14:13 +00:00
log.Fatalf("Failed to save config file: %v", err)
}
}