-
Notifications
You must be signed in to change notification settings - Fork 51
/
lint.go
104 lines (92 loc) · 2.72 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
94
95
96
97
98
99
100
101
102
103
104
package lint
import (
"fmt"
"path/filepath"
"github.com/jenkins-x-plugins/jx-gitops/pkg/apis/gitops/v1alpha1"
"github.com/jenkins-x-plugins/jx-gitops/pkg/rootcmd"
"github.com/jenkins-x/jx-api/v4/pkg/apis/core/v4beta1"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates"
"github.com/jenkins-x/jx-helpers/v3/pkg/linter"
"github.com/pkg/errors"
"github.com/roboll/helmfile/pkg/state"
"github.com/spf13/cobra"
)
var (
splitLong = templates.LongDesc(`
Lints the gitops files in the file system
`)
splitExample = templates.Examples(`
# lint files
%s lint --dir .
`)
// resourcesSeparator is used to separate multiple objects stored in the same YAML file
resourcesSeparator = "---\n"
)
// Options the options for the command
type Options struct {
linter.Options
Dir string
Verbose bool
Linters []linter.Linter
}
// NewCmdLint creates a command object for the command
func NewCmdLint() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "lint",
Short: "Lints the gitops files in the file system",
Long: splitLong,
Example: fmt.Sprintf(splitExample, rootcmd.BinaryName),
Run: func(cmd *cobra.Command, args []string) {
err := o.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&o.Dir, "dir", "d", ".", "the directory to recursively look for the *.yaml or *.yml files")
return cmd, o
}
// Validate verifies the configuration
func (o *Options) Validate() error {
o.Linters = append(o.Linters,
linter.Linter{
Path: filepath.Join(".jx", "gitops", v1alpha1.SourceConfigFileName),
Linter: func(path string, test *linter.Test) error {
return o.LintResource(path, test, &v1alpha1.SourceConfig{})
},
},
linter.Linter{
Path: filepath.Join("extensions", v1alpha1.PipelineCatalogFileName),
Linter: func(path string, test *linter.Test) error {
return o.LintResource(path, test, &v1alpha1.PipelineCatalog{})
},
},
linter.Linter{
Path: filepath.Join("extensions", v1alpha1.QuickstartsFileName),
Linter: func(path string, test *linter.Test) error {
return o.LintResource(path, test, &v1alpha1.Quickstarts{})
},
},
linter.Linter{
Path: filepath.Join(v4beta1.RequirementsConfigFileName),
Linter: func(path string, test *linter.Test) error {
return o.LintResource(path, test, &v4beta1.Requirements{})
},
},
linter.Linter{
Path: "helmfile.yaml",
Linter: func(path string, test *linter.Test) error {
return o.LintYaml2Resource(path, test, &state.HelmState{})
},
},
)
return nil
}
// Run implements the command
func (o *Options) Run() error {
err := o.Validate()
if err != nil {
return errors.Wrapf(err, "failed to validate")
}
return o.Lint(o.Linters, o.Dir)
}