-
Notifications
You must be signed in to change notification settings - Fork 18
/
validate.go
127 lines (107 loc) · 3.41 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package validate
import (
"errors"
"fmt"
"github.com/cirruslabs/cirrus-cli/internal/commands/helpers"
eenvironment "github.com/cirruslabs/cirrus-cli/internal/executor/environment"
"github.com/cirruslabs/cirrus-cli/internal/testutil"
"github.com/cirruslabs/cirrus-cli/pkg/parser"
"github.com/cirruslabs/cirrus-cli/pkg/rpcparser"
"github.com/spf13/cobra"
"log"
"strings"
)
var ErrValidate = errors.New("validate failed")
// General flags.
var validateFile string
var environment []string
// Experimental features flags.
var experimentalParser bool
var yaml bool
func validate(cmd *cobra.Command, args []string) error {
// https://github.com/spf13/cobra/issues/340#issuecomment-374617413
cmd.SilenceUsage = true
// Craft the environment
baseEnvironment := eenvironment.Merge(
eenvironment.Static(),
eenvironment.BuildID(),
eenvironment.ProjectSpecific("."),
)
userSpecifiedEnvironment := helpers.EnvArgsToMap(environment)
resultingEnvironment := eenvironment.Merge(baseEnvironment, userSpecifiedEnvironment)
// Retrieve a combined YAML configuration or a specific one if asked to
var configuration string
var err error
switch {
case validateFile == "":
configuration, err = helpers.ReadCombinedConfig(cmd.Context(), resultingEnvironment)
if err != nil {
return err
}
case strings.HasSuffix(validateFile, ".yml") || strings.HasSuffix(validateFile, ".yaml"):
configuration, err = helpers.ReadYAMLConfig(validateFile)
if err != nil {
return err
}
case strings.HasSuffix(validateFile, ".star"):
configuration, err = helpers.EvaluateStarlarkConfig(cmd.Context(), validateFile, resultingEnvironment)
if err != nil {
return err
}
default:
return ErrValidate
}
// Parse
var result *parser.Result
if experimentalParser {
p := parser.New(parser.WithEnvironment(userSpecifiedEnvironment))
result, err = p.Parse(cmd.Context(), configuration)
if err != nil {
return err
}
} else {
p := rpcparser.Parser{Environment: userSpecifiedEnvironment}
r, err := p.Parse(configuration)
if err != nil {
return err
}
// Convert into new parser result structure
result = &parser.Result{
Errors: r.Errors,
Tasks: r.Tasks,
}
}
// Check for errors
if len(result.Errors) > 0 {
for _, e := range result.Errors {
log.Println(e)
}
return ErrValidate
}
if yaml {
fmt.Println(string(testutil.TasksToJSON(nil, result.Tasks)))
}
return nil
}
func NewValidateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "validate",
Short: "Validate Cirrus CI configuration file",
RunE: validate,
}
// General flags
cmd.PersistentFlags().StringArrayVarP(&environment, "environment", "e", []string{},
"set (-e A=B) or pass-through (-e A) an environment variable to the Starlark interpreter")
cmd.PersistentFlags().StringVarP(&validateFile, "file", "f", "",
"use file as the configuration file (the path should end with either .yml or ..star)")
// Experimental features flags
cmd.PersistentFlags().BoolVar(&experimentalParser, "experimental-parser", false,
"use local configuration parser instead of sending parse request to Cirrus Cloud")
// A hidden flag to dump YAML representation of tasks and aid in generating test
// cases for smooth rpcparser → parser transition
cmd.PersistentFlags().BoolVar(&yaml, "json", false, "emit a JSON list with tasks contained in the configuration file")
if err := cmd.PersistentFlags().MarkHidden("json"); err != nil {
panic(err)
}
return cmd
}