/
validate.go
56 lines (47 loc) · 1.59 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package validatepipelinehelpers
import (
"fmt"
"github.com/concourse/concourse/atc"
"github.com/concourse/concourse/fly/commands/internal/displayhelpers"
"github.com/concourse/concourse/fly/commands/internal/templatehelpers"
"github.com/concourse/concourse/go-concourse/concourse"
"gopkg.in/yaml.v2"
)
func Validate(yamlTemplate templatehelpers.YamlTemplateWithParams, strict bool, output bool) error {
evaluatedTemplate, err := yamlTemplate.Evaluate(true, strict)
if err != nil {
return err
}
var unmarshalledTemplate atc.Config
if strict {
// UnmarshalStrict will pick up fields in structs that have the wrong names, as well as any duplicate keys in maps
// we should consider always using this everywhere in a later release...
if err := yaml.UnmarshalStrict([]byte(evaluatedTemplate), &unmarshalledTemplate); err != nil {
return err
}
} else {
if err := yaml.Unmarshal([]byte(evaluatedTemplate), &unmarshalledTemplate); err != nil {
return err
}
}
warnings, errorMessages := unmarshalledTemplate.Validate()
if len(warnings) > 0 {
configWarnings := make([]concourse.ConfigWarning, len(warnings))
for idx, warning := range warnings {
configWarnings[idx] = concourse.ConfigWarning(warning)
}
displayhelpers.ShowWarnings(configWarnings)
}
if len(errorMessages) > 0 {
displayhelpers.ShowErrors("Error loading existing config", errorMessages)
}
if len(errorMessages) > 0 || (strict && len(warnings) > 0) {
displayhelpers.Failf("configuration invalid")
}
if output {
fmt.Println(string(evaluatedTemplate))
} else {
fmt.Println("looks good")
}
return nil
}