From 2791422f77e908adb307f93d6bd3b75559cae9dd Mon Sep 17 00:00:00 2001 From: James Blair Date: Fri, 16 Jun 2023 21:47:46 +1200 Subject: [PATCH] Ensure goimports can be fixed individually. Signed-off-by: James Blair --- Makefile | 6 +++++- scripts/fix-goimports.sh | 28 ++++++++++++++++++++++++++++ scripts/fix.sh | 11 ----------- 3 files changed, 33 insertions(+), 12 deletions(-) create mode 100755 scripts/fix-goimports.sh diff --git a/Makefile b/Makefile index a27630393..200a1fbd1 100644 --- a/Makefile +++ b/Makefile @@ -65,7 +65,7 @@ fuzz: verify: verify-gofmt verify-bom verify-lint verify-dep verify-shellcheck verify-goword \ verify-govet verify-license-header verify-receiver-name verify-mod-tidy verify-shellcheck \ verify-shellws verify-proto-annotations verify-genproto verify-goimport verify-yamllint -fix: fix-bom fix-lint fix-yamllint +fix: fix-goimports fix-bom fix-lint fix-yamllint ./scripts/fix.sh .PHONY: verify-gofmt @@ -132,6 +132,10 @@ verify-genproto: verify-goimport: PASSES="goimport" ./scripts/test.sh +.PHONY: fix-goimports +fix-goimports: + ./scripts/fix-goimports.sh + .PHONY: verify-yamllint verify-yamllint: yamllint --config-file tools/.yamllint . diff --git a/scripts/fix-goimports.sh b/scripts/fix-goimports.sh new file mode 100755 index 000000000..2f1022186 --- /dev/null +++ b/scripts/fix-goimports.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +set -euo pipefail + +source ./scripts/test_lib.sh + +ROOTDIR=$(pwd) + +# To fix according to newer version of go: +# go get golang.org/dl/gotip +# gotip download +# GO_CMD="gotip" +GO_CMD="go" + +ROOTDIR=$(pwd) + +function go_imports_fix { + GOFILES=$(run ${GO_CMD} list --f "{{with \$d:=.}}{{range .GoFiles}}{{\$d.Dir}}/{{.}}{{\"\n\"}}{{end}}{{end}}" ./...) + TESTGOFILES=$(run ${GO_CMD} list --f "{{with \$d:=.}}{{range .TestGoFiles}}{{\$d.Dir}}/{{.}}{{\"\n\"}}{{end}}{{end}}" ./...) + cd "${ROOTDIR}/tools/mod" + echo "${GOFILES}" "${TESTGOFILES}" | grep -v '.gw.go' | grep -v '.pb.go' | xargs -n 100 go run golang.org/x/tools/cmd/goimports -w -local go.etcd.io +} + +log_callout -e "\\nFixing goimports for you...\n" + +run_for_modules go_imports_fix || exit 2 + +log_success -e "\\nSUCCESS: goimports are fixed :)" diff --git a/scripts/fix.sh b/scripts/fix.sh index 45520e113..7936a2cf3 100755 --- a/scripts/fix.sh +++ b/scripts/fix.sh @@ -8,8 +8,6 @@ go mod tidy source ./scripts/test_lib.sh source ./scripts/updatebom.sh -ROOTDIR=$(pwd) - # To fix according to newer version of go: # go get golang.org/dl/gotip # gotip download @@ -31,20 +29,11 @@ function bash_ws_fix { find ./ -name '*.sh.bak' -print0 | xargs -0 rm } -function go_imports_fix { - GOFILES=$(run ${GO_CMD} list --f "{{with \$d:=.}}{{range .GoFiles}}{{\$d.Dir}}/{{.}}{{\"\n\"}}{{end}}{{end}}" ./...) - TESTGOFILES=$(run ${GO_CMD} list --f "{{with \$d:=.}}{{range .TestGoFiles}}{{\$d.Dir}}/{{.}}{{\"\n\"}}{{end}}{{end}}" ./...) - cd "${ROOTDIR}/tools/mod" - echo "${GOFILES}" "${TESTGOFILES}" | grep -v '.gw.go' | grep -v '.pb.go' | xargs -n 100 go run golang.org/x/tools/cmd/goimports -w -local go.etcd.io -} - log_callout -e "\\nFixing etcd code for you...\n" run_for_modules mod_tidy_fix || exit 2 run_for_modules run ${GO_CMD} fmt || exit 2 run_for_module tests bom_fix || exit 2 -log_callout "Fixing goimports..." -run_for_modules go_imports_fix || exit 2 bash_ws_fix || exit 2