-
Notifications
You must be signed in to change notification settings - Fork 7
/
lint-install.go
391 lines (338 loc) · 10.6 KB
/
lint-install.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package main
import (
"bytes"
_ "embed"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
"github.com/karrick/godirwalk"
"k8s.io/klog/v2"
)
var (
dryRunFlag = flag.Bool("dry-run", false, "Display changes to make")
goFlag = flag.String("go", "error", "Level to lint Go with: [ignore, warn, error]")
shellFlag = flag.String("shell", "error", "Level to lint Shell with: [ignore, warn, error]")
dockerfileFlag = flag.String("dockerfile", "error", "Level to lint Dockerfile with: [ignore, warn, error]")
yamlFlag = flag.String("yaml", "error", "Level to lint YAML with: [ignore, warn, error]")
makeFileName = flag.String("makefile", "Makefile", "name of Makefile to update")
//go:embed .golangci.yml
goLintConfig []byte
//go:embed .yamllint
yamlLintConfig []byte
//go:embed Makefile.tmpl
makeTmpl string
)
type Language int
const (
Go Language = iota
Shell
Dockerfile
YAML
)
type Config struct {
Makefile string
Args string
Go string
Dockerfile string
Shell string
YAML string
LintCommands map[string]string
FixCommands map[string]string
}
// applicableLinters returns a list of languages with known linters within a given directory.
func applicableLinters(root string) (map[Language]bool, error) {
klog.Infof("Searching for linters to use for %s ...", root)
found := map[Language]bool{}
err := godirwalk.Walk(root, &godirwalk.Options{
Callback: func(path string, de *godirwalk.Dirent) error {
switch {
case strings.HasSuffix(path, ".go"):
found[Go] = true
case strings.HasSuffix(path, "Dockerfile"):
found[Dockerfile] = true
case strings.HasSuffix(path, ".sh"):
found[Shell] = true
case strings.HasSuffix(path, ".yml"), strings.HasSuffix(path, ".yaml"):
found[YAML] = true
}
return nil
},
Unsorted: true,
})
return found, err
}
// updateMakefile updates the Makefile within a project with lint rules.
func updateMakefile(root string, cfg Config, dryRun bool) (string, error) {
dest := filepath.Join(root, cfg.Makefile)
var existing []byte
var err error
if _, err = os.Stat(dest); err == nil {
klog.Infof("Found existing %s", dest)
existing, err = os.ReadFile(dest)
if err != nil {
return "", err
}
}
var newRules bytes.Buffer
t := template.Must(template.New("Makefile").Parse(makeTmpl))
if err = t.Execute(&newRules, cfg); err != nil {
return "", fmt.Errorf("execute: %w", err)
}
ignore := false
inserted := false
proposed := []byte{}
for x, line := range bytes.Split(existing, []byte("\n")) {
if bytes.HasPrefix(line, []byte("# BEGIN: lint-install")) {
ignore = true
inserted = true
proposed = append(proposed, newRules.Bytes()...)
continue
}
if bytes.HasPrefix(line, []byte("# END: lint-install")) {
ignore = false
continue
}
if ignore {
continue
}
if x > 0 {
proposed = append(proposed, []byte("\n")...)
}
proposed = append(proposed, line...)
}
if !inserted {
proposed = append(proposed, newRules.Bytes()...)
}
// Trim extraneous newlines
if len(existing) == 0 {
proposed = bytes.TrimLeft(proposed, "\n")
}
proposed = bytes.TrimRight(proposed, "\n")
proposed = append(proposed, byte('\n'))
edits := myers.ComputeEdits("Makefile", string(existing), string(proposed))
change := gotextdiff.ToUnified(filepath.Base(dest), filepath.Base(dest), string(existing), edits)
if !dryRun {
if err := os.WriteFile(dest, proposed, 0o600); err != nil {
return "", err
}
}
return fmt.Sprint(change), nil
}
// updateFile updates a configuration file within a project.
// If the content is nil the file is removed.
func updateFile(root string, basename string, content []byte, dryRun bool) (string, error) {
dest := filepath.Join(root, basename)
var existing []byte
f, err := os.Stat(dest)
if err == nil {
klog.Infof("Found existing %s", dest)
existing, err = os.ReadFile(dest)
if err != nil {
return "", err
}
}
proposed := string(content)
edits := myers.ComputeEdits(span.URI(basename), string(existing), proposed)
change := gotextdiff.ToUnified(basename, basename, string(existing), edits)
if !dryRun {
var err error
if content == nil {
// must be broken up, can't be conten == nil && f != nil because then
// the else branch will be taken if the file does *not* exist and
// an empty file will be written
if f != nil {
err = os.Remove(dest)
}
} else {
err = os.WriteFile(dest, content, 0o600)
}
if err != nil {
return "", err
}
}
return fmt.Sprint(change), nil
}
// updateGitignore updates the .gitignore file within a project to exclude installed linters.
func updateGitignore(root string, dryRun bool) (string, error) {
dest := filepath.Join(root, ".gitignore")
var existing []byte
var err error
if _, err = os.Stat(dest); err == nil {
klog.Infof("Found existing %s", dest)
existing, err = os.ReadFile(dest)
if err != nil {
return "", err
}
}
proposed := []byte{}
found := false
for _, line := range bytes.Split(existing, []byte("\n")) {
if bytes.Equal(line, []byte("out/")) || bytes.Equal(line, []byte("out")) {
found = true
}
proposed = append(proposed, line...)
proposed = append(proposed, []byte("\n")...)
}
if !found {
proposed = append(proposed, []byte("# added by lint-install\nout/\n")...)
}
// Trim extra newlines at the end
proposed = bytes.TrimRight(proposed, "\n")
proposed = append(proposed, byte('\n'))
edits := myers.ComputeEdits(".gitignore", string(existing), string(proposed))
change := gotextdiff.ToUnified(filepath.Base(dest), filepath.Base(dest), string(existing), edits)
if !dryRun {
if err := os.WriteFile(dest, proposed, 0o600); err != nil {
return "", err
}
}
return fmt.Sprint(change), nil
}
// goLintCmd returns the appropriate golangci-lint command to run for a project.
func goLintCmd(root string, level string, fix bool) string {
klog.Infof("Searching for go modules within %s ...", root)
found := []string{}
err := godirwalk.Walk(root, &godirwalk.Options{
Callback: func(path string, de *godirwalk.Dirent) error {
if strings.HasSuffix(path, "go.mod") {
found = append(found, filepath.Dir(path))
}
return nil
},
Unsorted: true,
})
if err != nil {
klog.Errorf("unable to find go.mod files: %v", err)
}
suffix := ""
if fix {
suffix = " --fix"
} else if level == "warn" {
suffix = " || true"
}
klog.Infof("found %d modules within %s: %s", len(found), root, found)
return fmt.Sprintf(`find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLANGCI_LINT_CONFIG)"%s \;`, suffix)
}
// shellLintCmd returns the appropriate shell lint command for a project.
func shellLintCmd(_ string, level string, fix bool) string {
suffix := ""
if fix {
// Use git apply instead of patch(1) because it doesn't support patching from stdin on all platforms.
// Everything after the first | is so we don't call git apply if there's nothing to apply, which would cause git apply to return an error.
// This is the cross platform way to do what gnu xargs does with its --no-run-if-empty switch.
// read -t 1 will read the first line from stdin but timeout after 1 second if empty
// if its not empty it will run the rest of the line after && which echoes the line and uses cat to pipe the rest to git
suffix = ` -f diff | { read -t 1 line || exit 0; { echo "$$line" && cat; } | git apply -p2; }`
} else if level == "warn" {
suffix = " || true"
}
return fmt.Sprintf(`$(SHELLCHECK_BIN) $(shell find . -name "*.sh")%s`, suffix)
}
// dockerLintCmd returns the appropriate docker lint command for a project.
func dockerLintCmd(_ string, level string) string {
f := ""
if level == "warn" {
f = " --no-fail"
}
return fmt.Sprintf(`$(HADOLINT_BIN)%s $(shell find . -name "*Dockerfile")`, f)
}
// yamlLintCmd returns the appropriate yamllint command for a project.
func yamlLintCmd(_ string, level string) string {
suffix := ""
if level == "warn" {
suffix = " || true"
}
return fmt.Sprintf(`PYTHONPATH=$(YAMLLINT_ROOT)/dist $(YAMLLINT_ROOT)/dist/bin/yamllint .%s`, suffix)
}
// main creates peanut butter & jelly sandwiches with utmost precision.
func main() {
klog.InitFlags(nil)
flag.Parse()
if len(flag.Args()) == 0 {
klog.Exitf("usage: lint-install [directory..]")
}
for _, root := range flag.Args() {
needs, err := applicableLinters(root)
if err != nil {
klog.Exitf("failed to find linters: %v", err)
}
if len(needs) == 0 {
continue
}
cfg := Config{
Args: strings.Join(os.Args[1:], " "),
Makefile: *makeFileName,
LintCommands: make(map[string]string),
FixCommands: make(map[string]string),
}
if needs[Go] {
cfg.Go = *goFlag
cfg.LintCommands["golangci-lint"] = goLintCmd(root, cfg.Go, false)
cfg.FixCommands["golangci-lint"] = goLintCmd(root, cfg.Go, true)
diff, err := updateFile(root, ".golangci.yml", goLintConfig, *dryRunFlag)
if err != nil {
klog.Exitf("update go lint config failed: %v", err)
}
if diff != "" {
klog.Infof("go lint config changes:\n%s", diff)
} else {
klog.Infof("go lint config has no changes")
}
for _, name := range []string{".golangci.json", ".golangci.toml", ".golangci.yaml"} {
diff, err := updateFile(root, name, nil, *dryRunFlag)
if err != nil {
klog.Exitf("deleting non-standardized go lint config failed: %v", err)
}
if diff != "" {
klog.Infof("standardizing on golangci.yml, deleting %s", name)
}
}
}
if needs[Dockerfile] {
cfg.Dockerfile = *dockerfileFlag
cfg.LintCommands["hadolint"] = dockerLintCmd(root, cfg.Dockerfile)
}
if needs[Shell] {
cfg.Shell = *shellFlag
cfg.LintCommands["shellcheck"] = shellLintCmd(root, cfg.Shell, false)
cfg.FixCommands["shellcheck"] = shellLintCmd(root, cfg.Shell, true)
}
if needs[YAML] {
cfg.YAML = *yamlFlag
cfg.LintCommands["yamllint"] = yamlLintCmd(root, cfg.Shell)
diff, err := updateFile(root, ".yamllint", yamlLintConfig, *dryRunFlag)
if err != nil {
klog.Exitf("update yaml lint config failed: %v", err)
}
if diff != "" {
klog.Infof("yamllint config changes:\n%s", diff)
} else {
klog.Infof("yamllint config has no changes")
}
}
diff, err := updateMakefile(root, cfg, *dryRunFlag)
if err != nil {
klog.Exitf("update Makefile failed: %v", err)
}
if diff != "" {
klog.Infof("Makefile changes:\n%s", diff)
} else {
klog.Infof("Makefile has no changes")
}
diff, err = updateGitignore(root, *dryRunFlag)
if err != nil {
klog.Exitf("update .gitignore failed: %v", err)
}
if diff != "" {
klog.Infof(".gitignore changes:\n%s", diff)
} else {
klog.Infof(".gitignore has no changes")
}
}
}