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 <z.bessarab@victoriametrics.com>

* make: add goimports install task

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

* 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 <z.bessarab@victoriametrics.com>

* make: update goimports to run on all changed files

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

---------

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>
This commit is contained in:
Zakhar Bessarab 2023-07-13 11:25:00 +04:00 committed by GitHub
parent 41b58a8e55
commit 20b18e9feb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -409,7 +409,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/...
@ -474,6 +484,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