Skip to content

Commit e87abfd

Browse files
authored
Merge pull request tinkerbell#16 from tstromberg/fix
Add 'fix' target to automatically fix lint issues
2 parents dfc0273 + 90ac55d commit e87abfd

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

Makefile

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

26+
fix: out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH)
27+
out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH) run --fix
28+
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh") -f diff | git apply -p2 -
29+
2630
out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck:
2731
mkdir -p out/linters
2832
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
@@ -19,7 +19,11 @@ endif
1919
{{ if .Go }}GOLINT_CONFIG:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))/.golangci.yml{{ end }}
2020

2121
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 }}
22-
{{- range .Commands }}
22+
{{- range .LintCommands }}
23+
{{ .}}{{ end}}
24+
25+
fix: {{ if .Shell }}out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck {{ end }}{{ if .Go}}out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH){{ end }}
26+
{{- range .FixCommands }}
2327
{{ .}}{{ end}}
2428

2529
{{ 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

@@ -193,11 +196,16 @@ func goLintCmd(root string, level string) string {
193196
}
194197

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

@@ -248,15 +256,17 @@ func main() {
248256

249257
if needs[Go] {
250258
cfg.Go = *goFlag
251-
cfg.Commands = append(cfg.Commands, goLintCmd(root, cfg.Go))
259+
cfg.LintCommands = append(cfg.LintCommands, goLintCmd(root, cfg.Go, false))
260+
cfg.FixCommands = append(cfg.FixCommands, goLintCmd(root, cfg.Go, true))
252261
}
253262
if needs[Dockerfile] {
254263
cfg.Dockerfile = *dockerfileFlag
255-
cfg.Commands = append(cfg.Commands, dockerLintCmd(root, cfg.Dockerfile))
264+
cfg.LintCommands = append(cfg.LintCommands, dockerLintCmd(root, cfg.Dockerfile))
256265
}
257266
if needs[Shell] {
258267
cfg.Shell = *shellFlag
259-
cfg.Commands = append(cfg.Commands, shellLintCmd(root, cfg.Shell))
268+
cfg.LintCommands = append(cfg.LintCommands, shellLintCmd(root, cfg.Shell, false))
269+
cfg.FixCommands = append(cfg.FixCommands, shellLintCmd(root, cfg.Shell, true))
260270
}
261271

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

0 commit comments

Comments
 (0)