forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.go
223 lines (201 loc) · 5.55 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
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
216
217
218
219
220
221
222
223
package cli
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path"
"runtime"
"sync"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
"github.com/benthosdev/benthos/v4/internal/config"
"github.com/benthosdev/benthos/v4/internal/docs"
ifilepath "github.com/benthosdev/benthos/v4/internal/filepath"
"github.com/benthosdev/benthos/v4/internal/filepath/ifs"
)
var (
red = color.New(color.FgRed).SprintFunc()
yellow = color.New(color.FgYellow).SprintFunc()
)
type pathLint struct {
source string
lint docs.Lint
}
func lintFile(path string, skipEnvVarCheck bool, lConf docs.LintConfig) (pathLints []pathLint) {
conf := config.New()
lints, err := config.ReadFileLinted(ifs.OS(), path, skipEnvVarCheck, lConf, &conf)
if err != nil {
pathLints = append(pathLints, pathLint{
source: path,
lint: docs.NewLintError(1, docs.LintFailedRead, err),
})
return
}
for _, l := range lints {
pathLints = append(pathLints, pathLint{
source: path,
lint: l,
})
}
return
}
func lintMDSnippets(path string, lConf docs.LintConfig) (pathLints []pathLint) {
rawBytes, err := ifs.ReadFile(ifs.OS(), path)
if err != nil {
pathLints = append(pathLints, pathLint{
source: path,
lint: docs.NewLintError(1, docs.LintFailedRead, err),
})
return
}
startTag, endTag := []byte("```yaml"), []byte("```")
nextSnippet := bytes.Index(rawBytes, startTag)
for nextSnippet != -1 {
nextSnippet += len(startTag)
snippetLine := bytes.Count(rawBytes[:nextSnippet], []byte("\n")) + 1
endOfSnippet := bytes.Index(rawBytes[nextSnippet:], endTag)
if endOfSnippet == -1 {
pathLints = append(pathLints, pathLint{
source: path,
lint: docs.NewLintError(snippetLine, docs.LintFailedRead, errors.New("markdown snippet not terminated")),
})
return
}
endOfSnippet = nextSnippet + endOfSnippet + len(endTag)
conf := config.New()
configBytes := rawBytes[nextSnippet : endOfSnippet-len(endTag)]
if err := yaml.Unmarshal(configBytes, &conf); err != nil {
var l docs.Lint
if errors.As(err, &l) {
l.Line += snippetLine - 1
pathLints = append(pathLints, pathLint{
source: path,
lint: l,
})
} else {
pathLints = append(pathLints, pathLint{
source: path,
lint: docs.NewLintError(snippetLine, docs.LintFailedRead, err),
})
}
} else {
lints, err := config.LintBytes(lConf, configBytes)
if err != nil {
pathLints = append(pathLints, pathLint{
source: path,
lint: docs.NewLintError(snippetLine, docs.LintFailedRead, err),
})
}
for _, l := range lints {
l.Line += snippetLine - 1
pathLints = append(pathLints, pathLint{
source: path,
lint: l,
})
}
}
if nextSnippet = bytes.Index(rawBytes[endOfSnippet:], []byte("```yaml")); nextSnippet != -1 {
nextSnippet += endOfSnippet
}
}
return
}
func lintCliCommand() *cli.Command {
return &cli.Command{
Name: "lint",
Usage: "Parse Benthos configs and report any linting errors",
Description: `
Exits with a status code 1 if any linting errors are detected:
benthos -c target.yaml lint
benthos lint ./configs/*.yaml
benthos lint ./foo.yaml ./bar.yaml
benthos lint ./configs/...
If a path ends with '...' then Benthos will walk the target and lint any
files with the .yaml or .yml extension.`[1:],
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "deprecated",
Value: false,
Usage: "Print linting errors for the presence of deprecated fields.",
},
&cli.BoolFlag{
Name: "labels",
Value: false,
Usage: "Print linting errors when components do not have labels.",
},
&cli.BoolFlag{
Name: "skip-env-var-check",
Value: false,
Usage: "Do not produce lint errors when environment interpolations exist without defaults within configs but aren't defined.",
},
},
Action: func(c *cli.Context) error {
if code := LintAction(c, os.Stderr); code != 0 {
os.Exit(code)
}
return nil
},
}
}
// LintAction performs the benthos lint subcommand and returns the appropriate
// exit code. This function is exported for testing purposes only.
func LintAction(c *cli.Context, stderr io.Writer) int {
targets, err := ifilepath.GlobsAndSuperPaths(ifs.OS(), c.Args().Slice(), "yaml", "yml")
if err != nil {
fmt.Fprintf(stderr, "Lint paths error: %v\n", err)
return 1
}
if conf := c.String("config"); len(conf) > 0 {
targets = append(targets, conf)
}
targets = append(targets, c.StringSlice("resources")...)
lConf := docs.NewLintConfig()
lConf.RejectDeprecated = c.Bool("deprecated")
lConf.RequireLabels = c.Bool("labels")
skipEnvVarCheck := c.Bool("skip-env-var-check")
var pathLintMut sync.Mutex
var pathLints []pathLint
threads := runtime.NumCPU()
var wg sync.WaitGroup
wg.Add(threads)
for i := 0; i < threads; i++ {
go func(threadID int) {
defer wg.Done()
for j, target := range targets {
if j%threads != threadID {
continue
}
if target == "" {
continue
}
var lints []pathLint
if path.Ext(target) == ".md" {
lints = lintMDSnippets(target, lConf)
} else {
lints = lintFile(target, skipEnvVarCheck, lConf)
}
if len(lints) > 0 {
pathLintMut.Lock()
pathLints = append(pathLints, lints...)
pathLintMut.Unlock()
}
}
}(i)
}
wg.Wait()
if len(pathLints) == 0 {
return 0
}
for _, lint := range pathLints {
lintText := fmt.Sprintf("%v%v\n", lint.source, lint.lint.Error())
if lint.lint.Type == docs.LintFailedRead || lint.lint.Type == docs.LintComponentMissing {
fmt.Fprint(stderr, red(lintText))
} else {
fmt.Fprint(stderr, yellow(lintText))
}
}
return 1
}