Skip to content

Commit 58f0bad

Browse files
committed
Merge branch 'main' into lint-path2
2 parents 1a72dcf + 8f846e1 commit 58f0bad

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

.golangci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,12 @@ issues:
173173
exclude-rules:
174174
- path: _test\.go
175175
linters:
176-
- gocyclo
177-
- errcheck
178176
- dupl
177+
- errcheck
178+
- forcetypeassert
179+
- gocyclo
179180
- gosec
181+
- noctx
180182

181183
- path: cmd.*
182184
linters:

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ lint: out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck out/l
2424
out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH) $(shell find . -name "*Dockerfile")
2525
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh")
2626

27+
fix: out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH)
28+
out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH) run --fix
29+
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh") -f diff | git apply -p2 -
30+
2731
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck:
2832
mkdir -p out/linters
2933
curl -sSfL https://github.com/koalaman/shellcheck/releases/download/$(SHELLCHECK_VERSION)/shellcheck-$(SHELLCHECK_VERSION).$(LINT_LOWER_OS).$(LINT_ARCH).tar.xz | tar -C out/linters -xJf -

Makefile.tmpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ endif
2020
{{ if .Go }}GOLINT_CONFIG:=$(LINT_ROOT)/.golangci.yml{{ end }}
2121

2222
lint: {{ if .Shell }}out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck {{ end }}{{ if .Dockerfile }}out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH) {{ end }}{{ if .Go}}out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH){{ end }}
23-
{{- range .Commands }}
23+
{{- range .LintCommands }}
24+
{{ .}}{{ end}}
25+
26+
fix: {{ if .Shell }}out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck {{ end }}{{ if .Go}}out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH){{ end }}
27+
{{- range .FixCommands }}
2428
{{ .}}{{ end}}
2529

2630
{{ if .Shell -}}

lint-install.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ const (
3939
)
4040

4141
type Config struct {
42-
Makefile string
43-
Args string
44-
Go string
45-
Dockerfile string
46-
Shell string
47-
Commands []string
42+
Makefile string
43+
Args string
44+
Go string
45+
Dockerfile string
46+
Shell string
47+
LintCommands []string
48+
FixCommands []string
4849
}
4950

5051
// applicableLinters returns a list of languages with known linters within a given directory.
@@ -163,7 +164,7 @@ func updateGoLint(root string, dryRun bool) (string, error) {
163164
}
164165

165166
// goLintCmd returns the appropriate golangci-lint command to run for a project.
166-
func goLintCmd(root string, level string) string {
167+
func goLintCmd(root string, level string, fix bool) string {
167168
klog.Infof("Searching for go modules within %s ...", root)
168169
found := []string{}
169170

@@ -181,7 +182,9 @@ func goLintCmd(root string, level string) string {
181182
}
182183

183184
suffix := ""
184-
if level == "warn" {
185+
if fix {
186+
suffix = " --fix"
187+
} else if level == "warn" {
185188
suffix = " || true"
186189
}
187190

@@ -194,11 +197,16 @@ func goLintCmd(root string, level string) string {
194197
}
195198

196199
// shellLintCmd returns the appropriate shell lint command for a project.
197-
func shellLintCmd(_ string, level string) string {
200+
func shellLintCmd(_ string, level string, fix bool) string {
198201
suffix := ""
199-
if level == "warn" {
202+
203+
if fix {
204+
// patch(1) doesn't support patching from stdin on all platforms, so we use git apply instead
205+
suffix = " -f diff | git apply -p2 -"
206+
} else if level == "warn" {
200207
suffix = " || true"
201208
}
209+
202210
return fmt.Sprintf(`out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh")%s`, suffix)
203211
}
204212

@@ -249,15 +257,17 @@ func main() {
249257

250258
if needs[Go] {
251259
cfg.Go = *goFlag
252-
cfg.Commands = append(cfg.Commands, goLintCmd(root, cfg.Go))
260+
cfg.LintCommands = append(cfg.LintCommands, goLintCmd(root, cfg.Go, false))
261+
cfg.FixCommands = append(cfg.FixCommands, goLintCmd(root, cfg.Go, true))
253262
}
254263
if needs[Dockerfile] {
255264
cfg.Dockerfile = *dockerfileFlag
256-
cfg.Commands = append(cfg.Commands, dockerLintCmd(root, cfg.Dockerfile))
265+
cfg.LintCommands = append(cfg.LintCommands, dockerLintCmd(root, cfg.Dockerfile))
257266
}
258267
if needs[Shell] {
259268
cfg.Shell = *shellFlag
260-
cfg.Commands = append(cfg.Commands, shellLintCmd(root, cfg.Shell))
269+
cfg.LintCommands = append(cfg.LintCommands, shellLintCmd(root, cfg.Shell, false))
270+
cfg.FixCommands = append(cfg.FixCommands, shellLintCmd(root, cfg.Shell, true))
261271
}
262272

263273
diff, err := updateMakefile(root, cfg, *dryRunFlag)

0 commit comments

Comments
 (0)