diff --git a/.golangci.yml b/.golangci.yml index 92c94a5348..46f1f3c959 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,6 +4,7 @@ linters: - depguard - dogsled - gocyclo + - gofumpt - goimports - gosec - gosimple diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 810be0ea9b..9feba24c88 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 diff --git a/Makefile b/Makefile index 8dd67d1e75..4a856962bb 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/docker.Makefile b/docker.Makefile index 562ef3757c..8a821a92c8 100644 --- a/docker.Makefile +++ b/docker.Makefile @@ -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 diff --git a/dockerfiles/Dockerfile.dev b/dockerfiles/Dockerfile.dev index f4dc9429fc..9416743f21 100644 --- a/dockerfiles/Dockerfile.dev +++ b/dockerfiles/Dockerfile.dev @@ -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/