-
Notifications
You must be signed in to change notification settings - Fork 12
/
validate.go
215 lines (181 loc) · 5.64 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// SPDX-License-Identifier: Apache-2.0
package pipeline
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/go-vela/cli/internal/output"
"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/server/compiler"
"github.com/sirupsen/logrus"
)
// Validate verifies the configuration provided.
func (c *Config) Validate() error {
logrus.Debug("validating pipeline configuration")
// handle the action based off the provided configuration
switch c.Action {
case "get":
// check if pipeline org is set
if len(c.Org) == 0 {
return fmt.Errorf("no pipeline org provided")
}
// check if pipeline repo is set
if len(c.Repo) == 0 {
return fmt.Errorf("no pipeline name provided")
}
case "compile":
fallthrough
case "expand":
fallthrough
case "view":
// check if pipeline org is set
if len(c.Org) == 0 {
return fmt.Errorf("no pipeline org provided")
}
// check if pipeline repo is set
if len(c.Repo) == 0 {
return fmt.Errorf("no pipeline name provided")
}
// check if pipeline ref is set
if len(c.Ref) == 0 {
return fmt.Errorf("no pipeline ref provided")
}
case "generate":
fallthrough
case "validate":
if len(c.Org) == 0 || len(c.Repo) == 0 {
// check if pipeline file is set
if len(c.File) == 0 {
return fmt.Errorf("no pipeline file provided")
}
}
for _, file := range c.TemplateFiles {
parts := strings.Split(file, ":")
if len(parts) != 2 {
return fmt.Errorf("invalid format for template file: %s (valid format: <name>:<source>)", file)
}
}
case "exec":
if strings.EqualFold(c.Event, constants.EventTag) && len(c.Tag) == 0 {
return fmt.Errorf("no tag provided for tag event")
}
}
return nil
}
// ValidateLocal verifies a local pipeline based off the provided configuration.
func (c *Config) ValidateLocal(client compiler.Engine) error {
logrus.Debug("executing validate for local pipeline configuration")
// send Filesystem call to capture base directory path
base, err := os.Getwd()
if err != nil {
return err
}
// create full path for pipeline file
path := filepath.Join(base, c.File)
// check if custom path was provided for pipeline file
if len(c.Path) > 0 {
// create custom full path for pipeline file
path = filepath.Join(c.Path, c.File)
}
// check if full path to pipeline file exists
path, err = validateFile(path)
if err != nil {
return err
}
// set pipelineType within client
client.WithRepo(&library.Repo{PipelineType: &c.PipelineType})
logrus.Tracef("compiling pipeline %s", path)
// compile the object into a pipeline
p, _, err := client.CompileLite(path, false)
if err != nil {
return err
}
// check to see if locally provided templates were included in compilation
for _, tmpl := range c.TemplateFiles {
parts := strings.Split(tmpl, ":")
included := false
for _, pTmpl := range p.Templates {
if strings.EqualFold(parts[0], pTmpl.Name) {
included = true
}
}
if !included {
return fmt.Errorf("local template with name %s not included in pipeline templates", parts[0])
}
}
// output the message in stderr format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stderr
err = output.Stderr(fmt.Sprintf("%s is valid", path))
if err != nil {
return err
}
// output the validated pipeline in stdout format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout
return output.YAML(p)
}
// ValidateRemote validates a remote pipeline based off the provided configuration.
func (c *Config) ValidateRemote(client *vela.Client) error {
logrus.Debug("executing validate for remote pipeline configuration")
logrus.Tracef("validating pipeline %s/%s@%s", c.Org, c.Repo, c.Ref)
// set the pipeline options for the call
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#PipelineOptions
opts := &vela.PipelineOptions{
Output: c.Output,
}
// send API call to validate a pipeline
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#PipelineService.Validate
pipeline, _, err := client.Pipeline.Validate(c.Org, c.Repo, c.Ref, opts)
if err != nil {
return err
}
// handle the output based off the provided configuration
switch c.Output {
case output.DriverDump:
// output the pipeline in dump format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump
return output.Dump(pipeline)
case output.DriverJSON:
// output the pipeline in JSON format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON
return output.JSON(pipeline)
case output.DriverSpew:
// output the pipeline in spew format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew
return output.Spew(pipeline)
case output.DriverYAML:
// output the pipeline in YAML format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML
return output.YAML(pipeline)
default:
// output the pipeline in stdout format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout
return output.Stdout(pipeline)
}
}
// validateFile validates the configuration file exists.
func validateFile(path string) (string, error) {
// check if file exists
if _, err := os.Stat(path); os.IsNotExist(err) {
// attempt to validate if .vela.yaml exists if .vela.yml does not
if filepath.Base(path) == ".vela.yml" {
// override path if .vela.yaml exists
if _, err := os.Stat(filepath.Join(filepath.Dir(path), ".vela.yaml")); err == nil {
return filepath.Join(filepath.Dir(path), ".vela.yaml"), nil
}
}
return path, fmt.Errorf("configuration file of %s does not exist", path)
}
return path, nil
}