-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.go
86 lines (78 loc) · 2.1 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
package commands
import (
"fmt"
"os"
"path/filepath"
"github.com/argoproj/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/argoproj/argo/cmd/argo/commands/client"
workflowpkg "github.com/argoproj/argo/pkg/apiclient/workflow"
"github.com/argoproj/argo/workflow/validate"
)
func NewLintCommand() *cobra.Command {
var (
strict bool
)
var command = &cobra.Command{
Use: "lint FILE...",
Short: "validate files or directories of workflow manifests",
Run: func(cmd *cobra.Command, args []string) {
ctx, apiClient := client.NewAPIClient()
serviceClient := apiClient.NewWorkflowServiceClient()
namespace := client.Namespace()
lint := func(file string) error {
wfs, err := validate.ParseWfFromFile(file, strict)
if err != nil {
return err
}
if len(wfs) == 0 {
return fmt.Errorf("there was nothing to validate")
}
for _, wf := range wfs {
_, err := serviceClient.LintWorkflow(ctx, &workflowpkg.WorkflowLintRequest{Namespace: namespace, Workflow: &wf})
if err != nil {
return err
}
}
fmt.Printf("%s is valid\n", file)
return nil
}
invalid := false
for _, file := range args {
stat, err := os.Stat(file)
errors.CheckError(err)
if stat.IsDir() {
_ = filepath.Walk(file, func(path string, info os.FileInfo, err error) error {
// If there was an error with the walk, return
if err != nil {
return err
}
fileExt := filepath.Ext(info.Name())
switch fileExt {
case ".yaml", ".yml", ".json":
default:
return nil
}
if err := lint(path); err != nil {
log.Errorf("Error in file %s: %s", path, err)
invalid = true
}
return nil
})
} else {
if err := lint(file); err != nil {
log.Errorf("Error in file %s: %s", file, err)
invalid = true
}
}
}
if invalid {
log.Fatalf("Errors encountered in validation")
}
fmt.Printf("Workflow manifests validated\n")
},
}
command.Flags().BoolVar(&strict, "strict", true, "perform strict workflow validation")
return command
}