2019-01-25 04:10:38 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2019-02-17 01:54:48 +00:00
|
|
|
"fmt"
|
2019-02-02 14:45:53 +00:00
|
|
|
"github.com/sqshq/sampler/console"
|
|
|
|
"github.com/sqshq/sampler/data"
|
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-02-17 01:54:48 +00:00
|
|
|
Theme *console.Theme `yaml:"theme,omitempty"`
|
|
|
|
RunCharts []RunChartConfig `yaml:"runcharts,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ComponentConfig struct {
|
|
|
|
Title string `yaml:"title"`
|
|
|
|
RefreshRateMs *int `yaml:"refresh-rate-ms,omitempty"`
|
|
|
|
Precision *int `yaml:"precision,omitempty"`
|
|
|
|
Position Position `yaml:"position"`
|
|
|
|
Size Size `yaml:"size"`
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 05:07:25 +00:00
|
|
|
type RunChartConfig struct {
|
2019-02-17 01:54:48 +00:00
|
|
|
ComponentConfig `yaml:",inline"`
|
|
|
|
Legend *LegendConfig `yaml:"legend,omitempty"`
|
|
|
|
Items []data.Item `yaml:"items"`
|
2019-01-25 04:10:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 04:34:45 +00:00
|
|
|
type LegendConfig struct {
|
|
|
|
Enabled bool `yaml:"enabled"`
|
|
|
|
Details bool `yaml:"details"`
|
|
|
|
}
|
|
|
|
|
2019-02-17 01:54:48 +00:00
|
|
|
type Position struct {
|
|
|
|
X int `yaml:"w"`
|
|
|
|
Y int `yaml:"h"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Size struct {
|
|
|
|
X int `yaml:"w"`
|
|
|
|
Y int `yaml:"h"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ComponentType rune
|
|
|
|
|
|
|
|
const (
|
|
|
|
TypeRunChart ComponentType = 0
|
|
|
|
TypeBarChart ComponentType = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
type ComponentSettings struct {
|
|
|
|
Type ComponentType
|
|
|
|
Title string
|
|
|
|
Size Size
|
|
|
|
Position Position
|
|
|
|
}
|
|
|
|
|
2019-02-15 04:34:45 +00:00
|
|
|
func Load() *Config {
|
2019-01-25 04:10:38 +00:00
|
|
|
|
2019-02-15 04:34:45 +00:00
|
|
|
if len(os.Args) < 2 {
|
|
|
|
println("Please specify config file location. See www.github.com/sqshq/sampler for the reference")
|
2019-02-13 01:46:18 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2019-02-15 04:34:45 +00:00
|
|
|
cfg := readFile(os.Args[1])
|
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
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2019-02-17 01:54:48 +00:00
|
|
|
func Update(settings []ComponentSettings) {
|
|
|
|
cfg := readFile(os.Args[1])
|
|
|
|
for _, s := range settings {
|
|
|
|
componentConfig := cfg.findComponent(s.Type, s.Title)
|
|
|
|
componentConfig.Size = s.Size
|
|
|
|
componentConfig.Position = s.Position
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-17 01:54:48 +00:00
|
|
|
|
|
|
|
func saveFile(config *Config) {
|
|
|
|
file, err := yaml.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Can't marshal config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile("config.yml", file, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Can't save config file: %v", err)
|
|
|
|
}
|
|
|
|
}
|