@@ -39,12 +39,13 @@ const (
3939)
4040
4141type 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