forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_pipeline.go
84 lines (65 loc) · 2.07 KB
/
validate_pipeline.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package commands
import (
"fmt"
"io/ioutil"
"os"
"github.com/concourse/atc"
"github.com/concourse/fly/commands/internal/displayhelpers"
"github.com/concourse/fly/template"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
)
type ValidatePipelineCommand struct {
Config atc.PathFlag `short:"c" long:"config" required:"true" description:"Pipeline configuration file"`
Strict bool `short:"s" long:"strict" description:"Fail on warnings"`
}
func (command *ValidatePipelineCommand) Execute(args []string) error {
configPath := command.Config
strict := command.Strict
configFile, err := ioutil.ReadFile(string(configPath))
if err != nil {
displayhelpers.FailWithErrorf("could not read config file", err)
}
configFile = template.EvaluateEmpty(configFile)
var configStructure interface{}
err = yaml.Unmarshal(configFile, &configStructure)
if err != nil {
displayhelpers.FailWithErrorf("failed to unmarshal configStructure", err)
}
var newConfig atc.Config
msConfig := &mapstructure.DecoderConfig{
Result: &newConfig,
WeaklyTypedInput: true,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
atc.SanitizeDecodeHook,
atc.VersionConfigDecodeHook,
),
}
decoder, err := mapstructure.NewDecoder(msConfig)
if err != nil {
displayhelpers.FailWithErrorf("failed to construct decoder", err)
}
if err := decoder.Decode(configStructure); err != nil {
displayhelpers.FailWithErrorf("failed to decode config", err)
}
warnings, errorMessages := newConfig.Validate()
if len(warnings) > 0 {
displayhelpers.PrintDeprecationWarningHeader()
for _, warning := range warnings {
fmt.Fprintf(os.Stderr, " - %s\n", warning.Message)
}
fmt.Fprintln(os.Stderr, "")
}
if len(errorMessages) > 0 {
displayhelpers.PrintWarningHeader()
for _, errorMessage := range errorMessages {
fmt.Fprintf(os.Stderr, " - %s\n", errorMessage)
}
fmt.Fprintln(os.Stderr, "")
}
if len(errorMessages) > 0 || (strict && len(warnings) > 0) {
displayhelpers.Failf("configuration invalid")
}
fmt.Println("looks good")
return nil
}