2019-01-25 04:10:38 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2019-02-17 01:54:48 +00:00
|
|
|
"fmt"
|
2019-03-19 03:02:02 +00:00
|
|
|
"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"
|
2019-02-13 01:46:18 +00:00
|
|
|
"os"
|
2019-01-25 04:10:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2019-03-24 22:46:39 +00:00
|
|
|
Theme *console.Theme `yaml:"theme,omitempty"`
|
2019-04-09 02:04:08 +00:00
|
|
|
Variables map[string]string `yaml:"variables,omitempty"`
|
2019-03-24 22:46:39 +00:00
|
|
|
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-02-17 01:54:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 02:58:46 +00:00
|
|
|
func LoadConfig() (*Config, Options) {
|
2019-02-25 00:08:36 +00:00
|
|
|
|
2019-03-19 03:02:02 +00:00
|
|
|
var opt Options
|
|
|
|
_, err := flags.Parse(&opt)
|
2019-01-25 04:10:38 +00:00
|
|
|
|
2019-03-19 03:02:02 +00:00
|
|
|
if err != nil {
|
2019-05-24 02:58:46 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.Version == true {
|
|
|
|
println(console.AppVersion)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt.ConfigFile == nil && opt.License == nil {
|
|
|
|
println("Please specify config file using --config flag. Example: sampler --config example.yml")
|
2019-03-17 00:54:24 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2019-02-13 01:46:18 +00:00
|
|
|
|
2019-05-24 02:58:46 +00:00
|
|
|
if opt.License != nil {
|
|
|
|
return nil, opt
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:02:02 +00:00
|
|
|
cfg := readFile(opt.ConfigFile)
|
2019-02-01 00:02:34 +00:00
|
|
|
cfg.validate()
|
2019-02-17 01:54:48 +00:00
|
|
|
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)
|
2019-02-17 01:54:48 +00:00
|
|
|
for _, s := range settings {
|
|
|
|
componentConfig := cfg.findComponent(s.Type, s.Title)
|
2019-03-18 02:41:23 +00:00
|
|
|
componentConfig.Position = getPosition(s.Location, s.Size)
|
2019-02-17 01:54:48 +00:00
|
|
|
}
|
2019-03-20 02:12:45 +00:00
|
|
|
saveFile(cfg, options.ConfigFile)
|
2019-02-17 01:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-03-24 22:46:39 +00:00
|
|
|
if component.Title == componentTitle {
|
2019-04-10 02:30:21 +00:00
|
|
|
return &c.TextBoxes[i].ComponentConfig
|
2019-03-24 22:46:39 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-17 01:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Sprintf(
|
2019-05-20 04:14:13 +00:00
|
|
|
"Failed to find component type %v with title %v", componentType, componentTitle))
|
2019-02-17 01:54:48 +00:00
|
|
|
}
|
|
|
|
|
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-02-17 01:54:48 +00:00
|
|
|
|
2019-05-24 02:58:46 +00:00
|
|
|
func saveFile(config *Config, fileName *string) {
|
2019-02-17 01:54:48 +00:00
|
|
|
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-02-17 01:54:48 +00:00
|
|
|
}
|
2019-05-24 02:58:46 +00:00
|
|
|
err = ioutil.WriteFile(*fileName, file, os.ModePerm)
|
2019-02-17 01:54:48 +00:00
|
|
|
if err != nil {
|
2019-05-20 04:14:13 +00:00
|
|
|
log.Fatalf("Failed to save config file: %v", err)
|
2019-02-17 01:54:48 +00:00
|
|
|
}
|
|
|
|
}
|