From 64149782acc1736f861e50a40f054fc9d26b6309 Mon Sep 17 00:00:00 2001 From: Zakhar Bessarab Date: Thu, 13 Jul 2023 11:25:00 +0400 Subject: [PATCH] make: add goimports task (#4582) * make: add goimports task Adds task to fix imports formatting implace. Formats imports into: - native library - external libraries - local packages based on github.com/VictoriaMetrics/VictoriaMetrics prefix Signed-off-by: Zakhar Bessarab * make: add goimports install task Signed-off-by: Zakhar Bessarab * make: run goimports only for changed files Applying goimports to all existing files would create a lot of problems with cherry-picking changes between different branches used for development. To avoid this it was decided to only run goimports on changed files to fix formatting gradually. Signed-off-by: Zakhar Bessarab * make: update goimports to run on all changed files Signed-off-by: Zakhar Bessarab --------- Signed-off-by: Zakhar Bessarab --- Makefile | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ba1e0fc887..63549581c4 100644 --- a/Makefile +++ b/Makefile @@ -170,7 +170,17 @@ vet: go vet ./lib/... go vet ./app/... -check-all: fmt vet golangci-lint govulncheck +# Set variables by using target specific variables to avoid running git diff for each make execution +# See: https://www.gnu.org/software/make/manual/html_node/Target_002dspecific.html +goimports: GO_CHANGED_FILES+=$(shell git diff --name-only --diff-filter=ACMR HEAD -- 'lib/*.go') +goimports: GO_CHANGED_FILES+=$(shell git diff --name-only --diff-filter=ACMR HEAD -- 'app/*.go') +goimports: install-goimports + # GO_CHANGED_FILES will contain a single space if there are no changed files + if [ "$(GO_CHANGED_FILES)" != " " ]; then \ + goimports -local github.com/VictoriaMetrics/VictoriaMetrics -w $(GO_CHANGED_FILES); \ + fi + +check-all: fmt goimports vet golangci-lint govulncheck test: go test ./lib/... ./app/... @@ -235,6 +245,9 @@ install-govulncheck: install-wwhrd: which wwhrd || go install github.com/frapposelli/wwhrd@latest +install-goimports: + which goimports || go install golang.org/x/tools/cmd/goimports@latest + check-licenses: install-wwhrd wwhrd check -f .wwhrd.yml