forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.go
228 lines (209 loc) · 5.25 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
224
225
226
227
228
package service
import (
"bytes"
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/dafanshu/benthos/v3/internal/docs"
"github.com/dafanshu/benthos/v3/lib/config"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)
var red = color.New(color.FgRed).SprintFunc()
var yellow = color.New(color.FgYellow).SprintFunc()
func resolveLintPath(path string) (string, bool) {
recurse := false
if path == "./..." || path == "..." {
recurse = true
path = "."
}
if strings.HasSuffix(path, "/...") {
recurse = true
path = strings.TrimSuffix(path, "/...")
}
return path, recurse
}
type pathLint struct {
source string
line int
lint string
err string
}
func lintFile(path string, rejectDeprecated bool) (pathLints []pathLint) {
conf := config.New()
lints, err := config.ReadV2(path, true, rejectDeprecated, &conf)
if err != nil {
pathLints = append(pathLints, pathLint{
source: path,
err: err.Error(),
})
return
}
for _, l := range lints {
pathLints = append(pathLints, pathLint{
source: path,
lint: l,
})
}
return
}
func lintMDSnippets(path string, rejectDeprecated bool) (pathLints []pathLint) {
rawBytes, err := os.ReadFile(path)
if err != nil {
pathLints = append(pathLints, pathLint{
source: path,
err: err.Error(),
})
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,
line: snippetLine,
err: "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 {
pathLints = append(pathLints, pathLint{
source: path,
line: snippetLine,
err: err.Error(),
})
} else {
lintCtx := docs.NewLintContext()
lintCtx.RejectDeprecated = rejectDeprecated
lints, err := config.LintV2(lintCtx, configBytes)
if err != nil {
pathLints = append(pathLints, pathLint{
source: path,
line: snippetLine,
err: err.Error(),
})
}
for _, l := range lints {
pathLints = append(pathLints, pathLint{
source: path,
line: snippetLine,
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.",
},
},
Action: func(c *cli.Context) error {
var targets []string
for _, p := range c.Args().Slice() {
var recurse bool
// TODO: V4 support wildcards
if p, recurse = resolveLintPath(p); recurse {
if err := filepath.Walk(p, func(path string, info os.FileInfo, werr error) error {
if werr != nil {
return werr
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, ".yaml") ||
strings.HasSuffix(path, ".yml") {
targets = append(targets, path)
}
return nil
}); err != nil {
fmt.Fprintf(os.Stderr, "Filesystem walk error: %v\n", err)
os.Exit(1)
}
} else {
targets = append(targets, p)
}
}
if conf := c.String("config"); len(conf) > 0 {
targets = append(targets, conf)
}
rejectDeprecated := c.Bool("deprecated")
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, rejectDeprecated)
} else {
lints = lintFile(target, rejectDeprecated)
}
if len(lints) > 0 {
pathLintMut.Lock()
pathLints = append(pathLints, lints...)
pathLintMut.Unlock()
}
}
}(i)
}
wg.Wait()
if len(pathLints) == 0 {
os.Exit(0)
}
for _, lint := range pathLints {
message := yellow(lint.lint)
if len(lint.err) > 0 {
message = red(lint.err)
}
if lint.line > 0 {
fmt.Fprintf(os.Stderr, "%v: from snippet at line %v: %v\n", lint.source, lint.line, message)
} else {
fmt.Fprintf(os.Stderr, "%v: %v\n", lint.source, message)
}
}
os.Exit(1)
return nil
},
}
}