Skip to content

Commit d56f6b1

Browse files
committed
Add 'fix' target to automatically fix lint issues
Signed-off-by: Thomas Stromberg <[email protected]>
1 parent dfc0273 commit d56f6b1

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
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 -

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)