Use gofumpt if available, and enable gofumpt linter

gofumpt provides a supserset of gofmt / go fmt, but not every developer may have
it installed, so for situations where it's not available, fall back to gofmt.

As our code has been formatted with gofumpt already, in most cases contributions
will follow those formatting rules, but in some cases there may be a difference,
which would already be flagged by manual code review, but let's also enable the
gofumpt linter.

With this change, `make fmt` will use gofumpt is available; gofumpt has been
added to the dev-container, so `make -f docker.Makefile fmt` will always use it.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-09-30 13:10:53 +02:00
Родитель 616124525e
Коммит c2f1671595
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 76698F39D527CE8C
5 изменённых файлов: 23 добавлений и 4 удалений

Просмотреть файл

@ -4,6 +4,7 @@ linters:
- depguard
- dogsled
- gocyclo
- gofumpt
- goimports
- gosec
- gosimple

Просмотреть файл

@ -333,7 +333,7 @@ mind when nudging others to comply.
The rules:
1. All code should be formatted with `gofmt -s`.
1. All code should be formatted with `gofumpt` (preferred) or `gofmt -s`.
2. All code should pass the default levels of
[`golint`](https://github.com/golang/lint).
3. All code should follow the guidelines covered in [Effective

Просмотреть файл

@ -5,6 +5,11 @@
# Sets the name of the company that produced the windows binary.
PACKAGER_NAME ?=
# The repository doesn't have a go.mod, but "go list", and "gotestsum"
# expect to be run from a module.
GO111MODULE=auto
export GO111MODULE
all: binary
_:=$(shell ./scripts/warn-outside-container $(MAKECMDGOALS))
@ -45,8 +50,12 @@ shellcheck: ## run shellcheck validation
find scripts/ contrib/completion/bash -type f | grep -v scripts/winresources | grep -v '.*.ps1' | xargs shellcheck
.PHONY: fmt
fmt: ## run gofmt
go list -f {{.Dir}} ./... | xargs gofmt -w -s -d
fmt: ## run gofumpt (if present) or gofmt
@if command -v gofumpt > /dev/null; then \
gofumpt -w -d -lang=1.19 . ; \
else \
go list -f {{.Dir}} ./... | xargs gofmt -w -s -d ; \
fi
.PHONY: binary
binary: ## build executable for Linux

Просмотреть файл

@ -76,7 +76,7 @@ shellcheck: ## run shellcheck validation
docker buildx bake shellcheck
.PHONY: fmt
fmt: ## run gofmt
fmt: ## run gofumpt
$(DOCKER_RUN) $(DEV_DOCKER_IMAGE_NAME) make fmt
.PHONY: vendor

Просмотреть файл

@ -8,6 +8,14 @@ FROM docker/buildx-bin:${BUILDX_VERSION} AS buildx
FROM golang:${GO_VERSION}-alpine AS golang
ENV CGO_ENABLED=0
FROM golang AS gofumpt
ARG GOFUMPT_VERSION=v0.4.0
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
--mount=type=tmpfs,target=/go/src/ \
GO111MODULE=on go install "mvdan.cc/gofumpt@${GOFUMPT_VERSION}" \
&& gofumpt --version
FROM golang AS gotestsum
ARG GOTESTSUM_VERSION=v0.4.0
RUN --mount=type=cache,target=/root/.cache/go-build \
@ -40,6 +48,7 @@ ENV DISABLE_WARN_OUTSIDE_CONTAINER=1
ENV PATH=$PATH:/go/src/github.com/docker/cli/build
COPY --from=buildx /buildx /usr/libexec/docker/cli-plugins/docker-buildx
COPY --from=gofumpt /go/bin/* /go/bin/
COPY --from=gotestsum /go/bin/* /go/bin/
COPY --from=goversioninfo /go/bin/* /go/bin/