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`.
This commit is contained in:
Sybren A. Stüvel 2025-03-24 18:12:25 +01:00
parent da4dd490d0
commit f6d1d85722
2 changed files with 28 additions and 2 deletions

View File

@ -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

View File

@ -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)
}