Skip to content

Commit 534619f

Browse files
authored
Merge pull request #401 from sapcc/golangci-lint
Run modernize as part of golangci-lint
2 parents 4a4c625 + 37fbb2b commit 534619f

File tree

4 files changed

+16
-51
lines changed

4 files changed

+16
-51
lines changed

.golangci.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ linters:
5757
- intrange
5858
- iotamixing
5959
- misspell
60+
- modernize
6061
- nilerr
6162
- nolintlint
6263
- nosprintfhostport
@@ -144,6 +145,9 @@ linters:
144145
enable-all: true
145146
nolintlint:
146147
require-specific: true
148+
perfsprint:
149+
# modernize generates nicer fix code
150+
concat-loop: false
147151
staticcheck:
148152
dot-import-whitelist:
149153
- github.com/majewsky/gg/option

Makefile

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ install-goimports: FORCE
4242
install-golangci-lint: FORCE
4343
@if ! hash golangci-lint 2>/dev/null; then printf "\e[1;36m>> Installing golangci-lint (this may take a while)...\e[0m\n"; go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest; fi
4444

45-
install-modernize: FORCE
46-
@if ! hash modernize 2>/dev/null; then printf "\e[1;36m>> Installing modernize (this may take a while)...\e[0m\n"; go install golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest; fi
47-
4845
install-shellcheck: FORCE
4946
@if ! hash shellcheck 2>/dev/null; then printf "\e[1;36m>> Installing shellcheck...\e[0m\n"; SHELLCHECK_ARCH=$(shell uname -m); if [[ "$$SHELLCHECK_ARCH" == "arm64" ]]; then SHELLCHECK_ARCH=aarch64; fi; SHELLCHECK_OS=$(shell uname -s | tr '[:upper:]' '[:lower:]'); SHELLCHECK_VERSION="stable"; if command -v curl >/dev/null 2>&1; then GET="curl -sLo-"; elif command -v wget >/dev/null 2>&1; then GET="wget -O-"; else echo "Didn't find curl or wget to download shellcheck"; exit 2; fi; $$GET "https://github.com/koalaman/shellcheck/releases/download/$$SHELLCHECK_VERSION/shellcheck-$$SHELLCHECK_VERSION.$$SHELLCHECK_OS.$$SHELLCHECK_ARCH.tar.xz" | tar -Jxf -; BIN=$$(go env GOBIN); if [[ -z $$BIN ]]; then BIN=$$(go env GOPATH)/bin; fi; install -Dm755 shellcheck-$$SHELLCHECK_VERSION/shellcheck -t "$$BIN"; rm -rf shellcheck-$$SHELLCHECK_VERSION; fi
5047

@@ -57,7 +54,7 @@ install-addlicense: FORCE
5754
install-reuse: FORCE
5855
@if ! hash reuse 2>/dev/null; then if ! hash pipx 2>/dev/null; then printf "\e[1;31m>> You are required to manually intervene to install reuse as go-makefile-maker cannot automatically resolve installing reuse on all setups.\e[0m\n"; printf "\e[1;31m>> The preferred way for go-makefile-maker to install python tools after nix-shell is pipx which could not be found. Either install pipx using your package manager or install reuse using your package manager if at least version 6 is available.\e[0m\n"; printf "\e[1;31m>> As your Python was likely installed by your package manager, just doing pip install --user sadly does no longer work as pip issues a warning about breaking your system. Generally running --break-system-packages with --user is safe to do but you should only run this command if you can resolve issues with it yourself: pip3 install --user --break-system-packages reuse\e[0m\n"; else printf "\e[1;36m>> Installing reuse...\e[0m\n"; pipx install reuse; fi; fi
5956

60-
prepare-static-check: FORCE install-golangci-lint install-modernize install-shellcheck install-go-licence-detector install-addlicense install-reuse
57+
prepare-static-check: FORCE install-goimports install-golangci-lint install-shellcheck install-go-licence-detector install-addlicense install-reuse
6158

6259
# To add additional flags or values (before the default ones), specify the variable in the environment, e.g. `GO_BUILDFLAGS='-tags experimental' make`.
6360
# To override the default flags or values, specify the variable on the command line, e.g. `make GO_BUILDFLAGS='-tags experimental'`.
@@ -109,10 +106,6 @@ run-golangci-lint: FORCE install-golangci-lint
109106
@golangci-lint config verify
110107
@golangci-lint run
111108

112-
run-modernize: FORCE install-modernize
113-
@printf "\e[1;36m>> modernize\e[0m\n"
114-
@modernize $(GO_TESTPKGS)
115-
116109
run-shellcheck: FORCE install-shellcheck
117110
@printf "\e[1;36m>> shellcheck\e[0m\n"
118111
@find . \( -path './vendor/*' -prune \) -o -type f \( -name '*.bash' -o -name '*.ksh' -o -name '*.zsh' -o -name '*.sh' -o -name '*.shlib' \) -exec shellcheck {} +
@@ -136,7 +129,7 @@ check-reuse: FORCE install-reuse
136129

137130
check-license-headers: FORCE check-addlicense check-reuse
138131

139-
__static-check: FORCE run-shellcheck run-golangci-lint run-modernize check-dependency-licenses check-license-headers
132+
__static-check: FORCE run-shellcheck run-golangci-lint check-dependency-licenses check-license-headers
140133

141134
static-check: FORCE
142135
@$(MAKE) --keep-going --no-print-directory __static-check
@@ -171,10 +164,6 @@ goimports: FORCE install-goimports
171164
@printf "\e[1;36m>> goimports -w -local https://github.com/sapcc/go-makefile-maker\e[0m\n"
172165
@goimports -w -local github.com/sapcc/go-makefile-maker $(patsubst $(shell awk '$$1 == "module" {print $$2}' go.mod)%,.%/*.go,$(shell go list ./...))
173166

174-
modernize: FORCE install-modernize
175-
@printf "\e[1;36m>> modernize -fix ./...\e[0m\n"
176-
@modernize -fix ./...
177-
178167
clean: FORCE
179168
git clean -dxf build
180169

@@ -208,7 +197,6 @@ help: FORCE
208197
@printf "\e[1mPrepare\e[0m\n"
209198
@printf " \e[36minstall-goimports\e[0m Install goimports required by goimports/static-check\n"
210199
@printf " \e[36minstall-golangci-lint\e[0m Install golangci-lint required by run-golangci-lint/static-check\n"
211-
@printf " \e[36minstall-modernize\e[0m Install modernize required by run-modernize/static-check\n"
212200
@printf " \e[36minstall-shellcheck\e[0m Install shellcheck required by run-shellcheck/static-check\n"
213201
@printf " \e[36minstall-go-licence-detector\e[0m Install-go-licence-detector required by check-dependency-licenses/static-check\n"
214202
@printf " \e[36minstall-addlicense\e[0m Install addlicense required by check-license-headers/license-headers/static-check\n"
@@ -223,7 +211,6 @@ help: FORCE
223211
@printf "\e[1mTest\e[0m\n"
224212
@printf " \e[36mcheck\e[0m Run the test suite (unit tests and golangci-lint).\n"
225213
@printf " \e[36mrun-golangci-lint\e[0m Install and run golangci-lint. Installing is used in CI, but you should probably install golangci-lint using your package manager.\n"
226-
@printf " \e[36mrun-modernize\e[0m Install and run modernize. Installing is used in CI, but you should probably install modernize using your package manager.\n"
227214
@printf " \e[36mrun-shellcheck\e[0m Install and run shellcheck. Installing is used in CI, but you should probably install shellcheck using your package manager.\n"
228215
@printf " \e[36mbuild/cover.out\e[0m Run tests and generate coverage report.\n"
229216
@printf " \e[36mbuild/cover.html\e[0m Generate an HTML file with source code annotations from the coverage report.\n"
@@ -238,7 +225,6 @@ help: FORCE
238225
@printf " \e[36mlicense-headers\e[0m Add (or overwrite) license headers on all non-vendored source code files.\n"
239226
@printf " \e[36mcheck-dependency-licenses\e[0m Check all dependency licenses using go-licence-detector.\n"
240227
@printf " \e[36mgoimports\e[0m Run goimports on all non-vendored .go files\n"
241-
@printf " \e[36mmodernize\e[0m Run modernize on all non-vendored .go files\n"
242228
@printf " \e[36mclean\e[0m Run git clean.\n"
243229

244230
.PHONY: FORCE

internal/golangcilint/golangci.yaml.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ linters:
5757
- intrange
5858
- iotamixing
5959
- misspell
60+
- modernize
6061
- nilerr
6162
- nolintlint
6263
- nosprintfhostport
@@ -157,6 +158,9 @@ linters:
157158
- {{ . }}
158159
{{- end }}
159160
{{- end }}
161+
perfsprint:
162+
# modernize generates nicer fix code
163+
concat-loop: false
160164
staticcheck:
161165
dot-import-whitelist:
162166
- github.com/majewsky/gg/option

internal/makefile/makefile.go

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,7 @@ endif
109109
target: "install-golangci-lint",
110110
recipe: installTool("golangci-lint", "github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest"),
111111
})
112-
prepare.addRule(rule{
113-
description: "Install modernize required by run-modernize/static-check",
114-
phony: true,
115-
target: "install-modernize",
116-
recipe: installTool("modernize", "golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest"),
117-
})
118-
prepareStaticRecipe = append(prepareStaticRecipe, "install-golangci-lint", "install-modernize")
112+
prepareStaticRecipe = append(prepareStaticRecipe, "install-goimports", "install-golangci-lint")
119113
}
120114

121115
if cfg.ShellCheck.Enabled.UnwrapOr(true) {
@@ -351,31 +345,19 @@ endif
351345
},
352346
})
353347

354-
// add target to run modernize
355-
test.addRule(rule{
356-
description: "Install and run modernize. Installing is used in CI, but you should probably install modernize using your package manager.",
357-
phony: true,
358-
target: "run-modernize",
359-
prerequisites: []string{"install-modernize"},
360-
recipe: []string{
361-
`@printf "\e[1;36m>> modernize\e[0m\n"`,
362-
`@modernize $(GO_TESTPKGS)`,
363-
},
364-
})
365-
366348
if cfg.ShellCheck.Enabled.UnwrapOr(true) {
367349
// add target to run shellcheck
368-
ignorePathArgs := ""
350+
var ignorePathArgs strings.Builder
369351
for _, path := range cfg.ShellCheck.AllIgnorePaths(cfg.Golang) {
370352
if strings.HasPrefix(path, "/") {
371353
logg.Fatal("ShellCheck ignore paths must not start with a slash, got: %s", path)
372354
}
373355

374356
// https://github.com/ludeeus/action-shellcheck/blob/master/action.yaml#L120-L124
375357
if !strings.HasPrefix(path, "./") {
376-
ignorePathArgs += fmt.Sprintf(" \\( -path '*/%s/*' -prune \\) -o", path)
358+
fmt.Fprintf(&ignorePathArgs, " \\( -path '*/%s/*' -prune \\) -o", path)
377359
}
378-
ignorePathArgs += fmt.Sprintf(" \\( -path '%s' -prune \\) -o", path)
360+
fmt.Fprintf(&ignorePathArgs, " \\( -path '%s' -prune \\) -o", path)
379361
}
380362
// partly taken from https://github.com/ludeeus/action-shellcheck/blob/master/action.yaml#L164-L196
381363
test.addRule(rule{
@@ -385,7 +367,7 @@ endif
385367
prerequisites: []string{"install-shellcheck"},
386368
recipe: []string{
387369
`@printf "\e[1;36m>> shellcheck\e[0m\n"`,
388-
fmt.Sprintf(`@find . %s -type f \( -name '*.bash' -o -name '*.ksh' -o -name '*.zsh' -o -name '*.sh' -o -name '*.shlib' \) -exec shellcheck %s {} +`, strings.TrimSpace(ignorePathArgs), cfg.ShellCheck.Opts),
370+
fmt.Sprintf(`@find . %s -type f \( -name '*.bash' -o -name '*.ksh' -o -name '*.zsh' -o -name '*.sh' -o -name '*.shlib' \) -exec shellcheck %s {} +`, strings.TrimSpace(ignorePathArgs.String()), cfg.ShellCheck.Opts),
389371
},
390372
})
391373
}
@@ -601,7 +583,7 @@ endif
601583
staticCheckPrerequisites := []string{"run-shellcheck"}
602584
if isGolang {
603585
// add target for static code checks
604-
staticCheckPrerequisites = append(staticCheckPrerequisites, "run-golangci-lint", "run-modernize")
586+
staticCheckPrerequisites = append(staticCheckPrerequisites, "run-golangci-lint")
605587
if cfg.License.CheckDependencies.UnwrapOr(isSAPCC) {
606588
staticCheckPrerequisites = append(staticCheckPrerequisites, "check-dependency-licenses")
607589
}
@@ -616,17 +598,6 @@ endif
616598
fmt.Sprintf(`@goimports -w -local %s %s`, sr.ModulePath, allSourceFilesExpr),
617599
},
618600
})
619-
620-
dev.addRule(rule{
621-
description: "Run modernize on all non-vendored .go files",
622-
phony: true,
623-
target: "modernize",
624-
prerequisites: []string{"install-modernize"},
625-
recipe: []string{
626-
`@printf "\e[1;36m>> modernize -fix ./...\e[0m\n"`,
627-
`@modernize -fix ./...`,
628-
},
629-
})
630601
}
631602

632603
if cfg.License.AddHeaders.UnwrapOr(isSAPCC) {

0 commit comments

Comments
 (0)