diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-08-20 17:26:12 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-08-31 08:53:02 +0300 |
commit | 1fd682939f5b2697e80dbe70cca057887ec2077d (patch) | |
tree | 1e1a8c774e5cc1f26fd78636c53878f068bfccf3 /Makefile | |
parent | c8311c7ddbe0a3c04851ebac4dad55ef24d9b725 (diff) |
Makefile: Switch from gitalyfmt to gofumpt
We have hosted our own formatter via the gitalyfmt package. While this
has served us alright, there are (subjectively) better alternatives to
having our own formatter.
One of these alternatives is gofumpt, which is a stricter version of
gofmt adding a few additional rules on top:
- No empty lines at the beginning or end of a function.
- No empty lines around a lone statement (or comment) in a block.
- No empty lines before a simple error check.
- Composite literals should use newlines consistently.
- Empty field lists should use a single line.
- std imports must be in a separate group at the top.
- Short case clauses should take a single line.
- Multiline top-level declarations must be separated by empty lines.
- Single var declarations should not be grouped with parentheses.
- Contiguous top-level declarations should be grouped together.
- Simple var-declaration statements should use short assignments.
- The -s code simplification flag is enabled by default.
- Octal integer literals should use the 0o prefix on modules using Go 1.13 and later.
- Comments which aren't Go directives should start with a
whitespace.
To me, these rules all sound agreeable and should enforce a stricter
coding style across the project. So let's switch over to use this
formatter and remove our own homegrown one.
Diffstat (limited to 'Makefile')
-rw-r--r-- | Makefile | 17 |
1 files changed, 8 insertions, 9 deletions
@@ -46,7 +46,7 @@ GIT_PREFIX ?= ${GIT_INSTALL_DIR} # Tools GIT := $(shell which git) GOIMPORTS := ${TOOLS_DIR}/goimports -GITALYFMT := ${TOOLS_DIR}/gitalyfmt +GOFUMPT := ${TOOLS_DIR}/gofumpt GOLANGCI_LINT := ${TOOLS_DIR}/golangci-lint GO_LICENSES := ${TOOLS_DIR}/go-licenses PROTOC := ${TOOLS_DIR}/protoc/bin/protoc @@ -71,6 +71,7 @@ GO_BUILD_TAGS := tracer_static,tracer_static_jaeger,tracer_static_stackdrive # Dependency versions GOLANGCI_LINT_VERSION ?= 1.42.0 GOCOVER_COBERTURA_VERSION ?= aaee18c8195c3f2d90e5ef80ca918d265463842a +GOFUMPT_VERSION ?= 0.1.1 GOIMPORTS_VERSION ?= 2538eef75904eff384a2551359968e40c207d9d2 GO_JUNIT_REPORT_VERSION ?= 984a47ca6b0a7d704c4b589852051b4d7865aa17 GO_LICENSES_VERSION ?= 73411c8fa237ccc6a75af79d0a5bc021c9487aad @@ -328,15 +329,15 @@ lint-strict: lint ${Q}GOLANGCI_LINT_CONFIG=$(SOURCE_DIR)/.golangci-strict.yml $(MAKE) lint .PHONY: check-formatting -check-formatting: ${GOIMPORTS} ${GITALYFMT} +check-formatting: ${GOIMPORTS} ${GOFUMPT} ${Q}${GOIMPORTS} -l $(call find_go_sources) | awk '{ print } END { if(NR>0) { print "goimports error, run make format"; exit(1) } }' - ${Q}${GITALYFMT} $(call find_go_sources) | awk '{ print } END { if(NR>0) { print "Formatting error, run make format"; exit(1) } }' + ${Q}${GOFUMPT} -l $(call find_go_sources) | awk '{ print } END { if(NR>0) { print "Formatting error, run make format"; exit(1) } }' .PHONY: format ## Run Go formatter and adjust imports. -format: ${GOIMPORTS} ${GITALYFMT} +format: ${GOIMPORTS} ${GOFUMPT} ${Q}${GOIMPORTS} -w -l $(call find_go_sources) - ${Q}${GITALYFMT} -w $(call find_go_sources) + ${Q}${GOFUMPT} -w $(call find_go_sources) ${Q}${GOIMPORTS} -w -l $(call find_go_sources) .PHONY: notice-up-to-date @@ -519,16 +520,14 @@ ${TOOLS_DIR}/%: GOBIN = ${TOOLS_DIR} ${TOOLS_DIR}/%: ${TOOLS_DIR}/%.version ${TOOLS_DIR}/.%/go.mod ${Q}cd ${TOOLS_DIR}/.$* && go get ${TOOL_PACKAGE}@${TOOL_VERSION} -# Tools hosted by Gitaly itself -${GITALYFMT}: | ${TOOLS_DIR} - ${Q}go build -o $@ ${SOURCE_DIR}/internal/cmd/gitalyfmt - ${PROTOC_GEN_GITALY}: proto | ${TOOLS_DIR} ${Q}go build -o $@ ${SOURCE_DIR}/proto/go/internal/cmd/protoc-gen-gitaly # External tools ${GOCOVER_COBERTURA}: TOOL_PACKAGE = github.com/t-yuki/gocover-cobertura ${GOCOVER_COBERTURA}: TOOL_VERSION = ${GOCOVER_COBERTURA_VERSION} +${GOFUMPT}: TOOL_PACKAGE = mvdan.cc/gofumpt +${GOFUMPT}: TOOL_VERSION = v${GOFUMPT_VERSION} ${GOIMPORTS}: TOOL_PACKAGE = golang.org/x/tools/cmd/goimports ${GOIMPORTS}: TOOL_VERSION = ${GOIMPORTS_VERSION} ${GOLANGCI_LINT}: TOOL_PACKAGE = github.com/golangci/golangci-lint/cmd/golangci-lint |