sampler-fork/config/validator.go

79 lines
2.2 KiB
Go
Raw Permalink Normal View History

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"
)
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 {
validateItemScripts(title, i)
2019-06-23 04:08:24 +00:00
}
}
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 for '%s'", title))
}
if i.SampleScript == nil {
console.Exit(fmt.Sprintf("Config validation error: sample script should be specified for '%s'", title))
2019-06-23 04:08:24 +00:00
}
}
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' for '%s'", label, title))
2019-06-09 19:00:16 +00:00
}
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
}