forked from argoproj/argo-workflows
/
lint.go
67 lines (61 loc) · 1.64 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
package commands
import (
"fmt"
"os"
"path/filepath"
"github.com/argoproj/pkg/errors"
"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
}
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
}
for _, file := range args {
stat, err := os.Stat(file)
errors.CheckError(err)
if stat.IsDir() {
err := filepath.Walk(file, func(path string, info os.FileInfo, err error) error {
fileExt := filepath.Ext(info.Name())
switch fileExt {
case ".yaml", ".yml", ".json":
default:
return nil
}
return lint(path)
})
errors.CheckError(err)
} else {
err := lint(file)
errors.CheckError(err)
}
}
fmt.Printf("Workflow manifests validated\n")
},
}
command.Flags().BoolVar(&strict, "strict", true, "perform strict workflow validation")
return command
}