@@ -51,8 +51,8 @@ type Config struct {
5151 Dockerfile string
5252 Shell string
5353 YAML string
54- LintCommands [ ]string
55- FixCommands [ ]string
54+ LintCommands map [ string ]string
55+ FixCommands map [ string ]string
5656}
5757
5858// applicableLinters returns a list of languages with known linters within a given directory.
@@ -255,24 +255,28 @@ func goLintCmd(root string, level string, fix bool) string {
255255
256256 klog .Infof ("found %d modules within %s: %s" , len (found ), root , found )
257257 if len (found ) == 0 || (len (found ) == 1 && found [0 ] == strings .Trim (root , "/" )) {
258- return fmt .Sprintf ("out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH ) run%s" , suffix )
258+ return fmt .Sprintf ("$(GOLANGCI_LINT_BIN ) run%s" , suffix )
259259 }
260260
261- return fmt .Sprintf (`find . -name go.mod -execdir "$(LINT_ROOT)/out/linters/golangci-lint-$(GOLINT_VERSION)-$(LINT_ARCH )" run -c "$(GOLINT_CONFIG)"%s \;` , suffix )
261+ return fmt .Sprintf (`find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN )" run -c "$(GOLINT_CONFIG)"%s \;` , suffix )
262262}
263263
264264// shellLintCmd returns the appropriate shell lint command for a project.
265265func shellLintCmd (_ string , level string , fix bool ) string {
266266 suffix := ""
267267
268268 if fix {
269- // patch(1) doesn't support patching from stdin on all platforms, so we use git apply instead
270- suffix = " -f diff | git apply -p2 -"
269+ // Use git apply instead of patch(1) because it doesn't support patching from stdin on all platforms.
270+ // 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.
271+ // This is the cross platform way to do what gnu xargs does with its --no-run-if-empty switch.
272+ // read -t 1 will read the first line from stdin but timeout after 1 second if empty
273+ // 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
274+ suffix = ` -f diff | { read -t 1 line || exit 0; { echo "$$line" && cat; } | git apply -p2; }`
271275 } else if level == "warn" {
272276 suffix = " || true"
273277 }
274278
275- return fmt .Sprintf (`out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)/shellcheck $(shell find . -name "*.sh")%s` , suffix )
279+ return fmt .Sprintf (`$(SHELLCHECK_BIN) $(shell find . -name "*.sh")%s` , suffix )
276280}
277281
278282// dockerLintCmd returns the appropriate docker lint command for a project.
@@ -282,7 +286,7 @@ func dockerLintCmd(_ string, level string) string {
282286 f = " --no-fail"
283287 }
284288
285- return fmt .Sprintf (`out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH )%s $(shell find . -name "*Dockerfile")` , f )
289+ return fmt .Sprintf (`$(HADOLINT_BIN )%s $(shell find . -name "*Dockerfile")` , f )
286290}
287291
288292// yamlLintCmd returns the appropriate yamllint command for a project.
@@ -313,14 +317,16 @@ func main() {
313317 }
314318
315319 cfg := Config {
316- Args : strings .Join (os .Args [1 :], " " ),
317- Makefile : * makeFileName ,
320+ Args : strings .Join (os .Args [1 :], " " ),
321+ Makefile : * makeFileName ,
322+ LintCommands : make (map [string ]string ),
323+ FixCommands : make (map [string ]string ),
318324 }
319325
320326 if needs [Go ] {
321327 cfg .Go = * goFlag
322- cfg .LintCommands = append ( cfg . LintCommands , goLintCmd (root , cfg .Go , false ) )
323- cfg .FixCommands = append ( cfg . FixCommands , goLintCmd (root , cfg .Go , true ) )
328+ cfg .LintCommands [ "golangci-lint" ] = goLintCmd (root , cfg .Go , false )
329+ cfg .FixCommands [ "golangci-lint" ] = goLintCmd (root , cfg .Go , true )
324330
325331 diff , err := updateFile (root , ".golangci.yml" , goLintConfig , * dryRunFlag )
326332 if err != nil {
@@ -344,16 +350,16 @@ func main() {
344350 }
345351 if needs [Dockerfile ] {
346352 cfg .Dockerfile = * dockerfileFlag
347- cfg .LintCommands = append ( cfg . LintCommands , dockerLintCmd (root , cfg .Dockerfile ) )
353+ cfg .LintCommands [ "hadolint" ] = dockerLintCmd (root , cfg .Dockerfile )
348354 }
349355 if needs [Shell ] {
350356 cfg .Shell = * shellFlag
351- cfg .LintCommands = append ( cfg . LintCommands , shellLintCmd (root , cfg .Shell , false ) )
352- cfg .FixCommands = append ( cfg . FixCommands , shellLintCmd (root , cfg .Shell , true ) )
357+ cfg .LintCommands [ "shellcheck" ] = shellLintCmd (root , cfg .Shell , false )
358+ cfg .FixCommands [ "shellcheck" ] = shellLintCmd (root , cfg .Shell , true )
353359 }
354360 if needs [YAML ] {
355361 cfg .YAML = * yamlFlag
356- cfg .LintCommands = append ( cfg . LintCommands , yamlLintCmd (root , cfg .Shell ) )
362+ cfg .LintCommands [ "yamllint" ] = yamlLintCmd (root , cfg .Shell )
357363
358364 diff , err := updateFile (root , ".yamllint" , yamlLintConfig , * dryRunFlag )
359365 if err != nil {
0 commit comments