From f6d1d857225d4936b728aeb141327262f8c22b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Mon, 24 Mar 2025 18:12:25 +0100 Subject: [PATCH] Mage: Add `gofmt` to the Mage commands Add two new mage commands: - `mage format`: run `gofmt` on all the Go sources. - `mage formatCheck`: run the formatter to check formatting issues. This will output the diff to make the code format-compliant. These commands are also available via `make format` resp. `make format-check`. --- Makefile | 8 +++++++- magefiles/check.go | 22 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 80fa1396..31a730d7 100644 --- a/Makefile +++ b/Makefile @@ -154,6 +154,12 @@ devserver-website: devserver-webapp: buildtool "${BUILDTOOL_PATH}" devServerWebapp +format: buildtool + "${BUILDTOOL_PATH}" format + +format-check: buildtool + "${BUILDTOOL_PATH}" formatCheck + deploy-website: $(MAKE) -s check-environment rm -rf web/project-website/public/ @@ -311,4 +317,4 @@ publish-release-packages: ${RELEASE_PACKAGE_LINUX} ${RELEASE_PACKAGE_DARWIN} ${RELEASE_PACKAGE_DARWIN_ARM64} ${RELEASE_PACKAGE_WINDOWS} ${RELEASE_PACKAGE_SHAFILE} \ ${WEBSERVER_SSH}:${WEBSERVER_ROOT}/downloads/ -.PHONY: application version flamenco-manager flamenco-worker webapp webapp-static generate generate-go generate-py with-deps swagger-ui list-embedded test clean flamenco-manager-without-webapp +.PHONY: application version flamenco-manager flamenco-worker webapp webapp-static generate generate-go generate-py with-deps swagger-ui list-embedded test clean flamenco-manager-without-webapp format format-check diff --git a/magefiles/check.go b/magefiles/check.go index 4e179653..fd4bff1d 100644 --- a/magefiles/check.go +++ b/magefiles/check.go @@ -20,7 +20,7 @@ import ( // Run unit tests, check for vulnerabilities, and run the linter func Check(ctx context.Context) { - mg.CtxDeps(ctx, Test, Govulncheck, Staticcheck, Vet) + mg.CtxDeps(ctx, Test, Govulncheck, Staticcheck, Vet, FormatCheck) } // Run unit tests @@ -59,3 +59,23 @@ func Staticcheck() error { func Vet() error { return sh.RunV(mg.GoCmd(), "vet", "./...") } + +// Run `gofmt`, formatting all the source code. +func Format() error { + return sh.RunV("gofmt", "-s", "-w", ".") +} + +// Run `gofmt` on all the source code, reporting all differences. +func FormatCheck(ctx context.Context) error { + output, err := sh.Output("gofmt", "-d", ".") + if err != nil { + return err + } + + if output == "" { + // Format was OK. + return nil + } + + return fmt.Errorf("Formatting check failed:\n%s", output) +}