forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.go
93 lines (86 loc) · 2.23 KB
/
lint.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
package commands
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/argoproj/argo/errors"
cmdutil "github.com/argoproj/argo/util/cmd"
"github.com/argoproj/argo/workflow/common"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
RootCmd.AddCommand(lintCmd)
}
var lintCmd = &cobra.Command{
Use: "lint (DIRECTORY | FILE1 FILE2 FILE3...)",
Short: "validate a directory or specific workflow YAML files",
Run: lintYAML,
}
func lintYAML(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
validateDir := cmdutil.MustIsDir(args[0])
var err error
if validateDir {
if len(args) > 1 {
fmt.Printf("Validation of a single directory supported")
os.Exit(1)
}
fmt.Printf("Verifying all yaml files in directory: %s\n", args[0])
err = lintYAMLDir(args[0])
} else {
yamlFiles := make([]string, 0)
for _, filePath := range args {
if cmdutil.MustIsDir(filePath) {
fmt.Printf("Validate against a list of files or a single directory, not both")
os.Exit(1)
}
yamlFiles = append(yamlFiles, filePath)
}
for _, yamlFile := range yamlFiles {
err = lintYAMLFile(yamlFile)
if err != nil {
break
}
}
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("YAML validated\n")
}
func lintYAMLDir(dirPath string) error {
walkFunc := func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}
fileExt := filepath.Ext(info.Name())
if fileExt != ".yaml" && fileExt != ".yml" {
return nil
}
return lintYAMLFile(path)
}
return filepath.Walk(dirPath, walkFunc)
}
// lintYAMLFile lints multiple workflow manifest in a single yaml file. Ignores non-workflow manifests
func lintYAMLFile(filePath string) error {
body, err := ioutil.ReadFile(filePath)
if err != nil {
return errors.Errorf(errors.CodeBadRequest, "Can't read from file: %s, err: %v", filePath, err)
}
workflows, err := splitYAMLFile(body)
if err != nil {
return errors.Errorf(errors.CodeBadRequest, "%s failed to parse: %v", filePath, err)
}
for _, wf := range workflows {
err = common.ValidateWorkflow(&wf)
if err != nil {
return errors.Errorf(errors.CodeBadRequest, "%s: %s", filePath, err.Error())
}
}
return nil
}