2019-02-01 00:02:34 +00:00
|
|
|
package config
|
|
|
|
|
2019-06-09 19:00:16 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/sqshq/sampler/console"
|
|
|
|
)
|
|
|
|
|
2019-02-17 01:54:48 +00:00
|
|
|
func (c *Config) validate() {
|
2019-02-01 00:02:34 +00:00
|
|
|
|
2019-06-09 19:00:16 +00:00
|
|
|
var components []ComponentConfig
|
|
|
|
|
|
|
|
for _, c := range c.RunCharts {
|
|
|
|
components = append(components, c.ComponentConfig)
|
|
|
|
validateLabelsUniqueness(c.Title, c.Items)
|
2019-06-23 04:08:24 +00:00
|
|
|
validateItemsScripts(c.Title, c.Items)
|
2019-06-09 19:00:16 +00:00
|
|
|
}
|
|
|
|
for _, c := range c.BarCharts {
|
|
|
|
components = append(components, c.ComponentConfig)
|
|
|
|
validateLabelsUniqueness(c.Title, c.Items)
|
2019-06-23 04:08:24 +00:00
|
|
|
validateItemsScripts(c.Title, c.Items)
|
2019-06-09 19:00:16 +00:00
|
|
|
}
|
|
|
|
for _, c := range c.SparkLines {
|
|
|
|
components = append(components, c.ComponentConfig)
|
2019-06-23 04:08:24 +00:00
|
|
|
validateItemScripts(c.Title, c.Item)
|
2019-06-09 19:00:16 +00:00
|
|
|
}
|
|
|
|
for _, c := range c.Gauges {
|
|
|
|
components = append(components, c.ComponentConfig)
|
2019-06-23 04:08:24 +00:00
|
|
|
validateItemScripts(c.Title, c.Min)
|
|
|
|
validateItemScripts(c.Title, c.Max)
|
|
|
|
validateItemScripts(c.Title, c.Cur)
|
2019-06-09 19:00:16 +00:00
|
|
|
}
|
|
|
|
for _, c := range c.AsciiBoxes {
|
|
|
|
components = append(components, c.ComponentConfig)
|
2019-06-23 04:08:24 +00:00
|
|
|
validateItemScripts(c.Title, c.Item)
|
2019-06-09 19:00:16 +00:00
|
|
|
}
|
|
|
|
for _, c := range c.TextBoxes {
|
|
|
|
components = append(components, c.ComponentConfig)
|
2019-06-23 04:08:24 +00:00
|
|
|
validateItemScripts(c.Title, c.Item)
|
2019-06-09 19:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
validateTitlesUniqueness(components)
|
|
|
|
}
|
|
|
|
|
2019-06-23 04:08:24 +00:00
|
|
|
func validateItemsScripts(title string, items []Item) {
|
|
|
|
for _, i := range items {
|
|
|
|
if i.InitScript != nil && i.MultiStepInitScript != nil {
|
|
|
|
validateItemScripts(title, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateItemScripts(title string, i Item) {
|
|
|
|
if i.InitScript != nil && i.MultiStepInitScript != nil {
|
|
|
|
console.Exit(fmt.Sprintf("Config validation error: both init and multistep-init scripts are not allowed in '%s'", title))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 19:00:16 +00:00
|
|
|
func validateLabelsUniqueness(title string, items []Item) {
|
|
|
|
labels := make(map[string]bool)
|
2019-06-23 04:08:24 +00:00
|
|
|
for _, i := range items {
|
|
|
|
label := *i.Label
|
2019-06-09 19:00:16 +00:00
|
|
|
if _, contains := labels[label]; contains {
|
|
|
|
console.Exit(fmt.Sprintf("Config validation error: item labels should be unique. Please rename '%s' in '%s'", label, title))
|
|
|
|
}
|
|
|
|
labels[label] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateTitlesUniqueness(components []ComponentConfig) {
|
|
|
|
titles := make(map[string]bool)
|
|
|
|
for _, c := range components {
|
|
|
|
if _, contains := titles[c.Title]; contains {
|
|
|
|
console.Exit(fmt.Sprintf("Config validation error: component titles should be unique. Please rename '%s'", c.Title))
|
|
|
|
}
|
|
|
|
titles[c.Title] = true
|
|
|
|
}
|
2019-02-01 00:02:34 +00:00
|
|
|
}
|