From d25c157b20f6905fa06c43f4bd4044c0bc68ccc5 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sun, 13 Oct 2024 12:52:44 +0100 Subject: [PATCH] check formatting in ci --- .forgejo/workflows/workflow.yaml | 12 ++++++++-- cmd/indieauth-server/main.go | 4 +--- internal/executors/command.go | 5 +--- magefiles/mage.go | 40 ++++++++++++++++++++++++++++---- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/.forgejo/workflows/workflow.yaml b/.forgejo/workflows/workflow.yaml index 5e4f4fd..511e196 100644 --- a/.forgejo/workflows/workflow.yaml +++ b/.forgejo/workflows/workflow.yaml @@ -3,6 +3,8 @@ name: CI on: pull_request: + branches: + - main types: - opened - synchronize @@ -15,14 +17,14 @@ jobs: steps: - name: Checkout Repository uses: https://code.forgejo.org/actions/checkout@v4 - - name: Test + - name: Run tests uses: https://codeflow.dananglin.me.uk/actions/mage-ci@main with: target: test env: PROJECT_TEST_VERBOSE: "1" PROJECT_TEST_COVER: "1" - - name: Gosec + - name: Run gosec uses: https://codeflow.dananglin.me.uk/actions/mage-ci@main with: target: gosec @@ -34,6 +36,12 @@ jobs: steps: - name: Checkout Repository uses: https://code.forgejo.org/actions/checkout@v4 + - name: Check formatting + uses: https://codeflow.dananglin.me.uk/actions/mage-ci@main + with: + target: gofmt + env: + PROJECT_FAIL_ON_FORMATTING: 1 - name: Run staticcheck uses: https://codeflow.dananglin.me.uk/actions/mage-ci@main with: diff --git a/cmd/indieauth-server/main.go b/cmd/indieauth-server/main.go index 327bb4b..c959079 100644 --- a/cmd/indieauth-server/main.go +++ b/cmd/indieauth-server/main.go @@ -18,9 +18,7 @@ func main() { // Set up logging loggingLevel := new(slog.LevelVar) - slogOpts := slog.HandlerOptions{ - Level: loggingLevel, - } + slogOpts := slog.HandlerOptions{Level: loggingLevel} logger := slog.New(slog.NewTextHandler(os.Stdout, &slogOpts)) slog.SetDefault(logger) diff --git a/internal/executors/command.go b/internal/executors/command.go index 3c6b434..5712068 100644 --- a/internal/executors/command.go +++ b/internal/executors/command.go @@ -20,8 +20,5 @@ func newCommand(args []string) command { } } - return command{ - name: args[0], - args: args[1:], - } + return command{name: args[0], args: args[1:]} } diff --git a/magefiles/mage.go b/magefiles/mage.go index 05df9d7..cdd1d29 100644 --- a/magefiles/mage.go +++ b/magefiles/mage.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "time" "github.com/magefile/mage/mg" @@ -16,11 +17,13 @@ import ( const ( app = "indieauth-server" defaultInstallPrefix = "/usr/local" - envInstallPrefix = "PROJECT_INSTALL_PREFIX" - envTestVerbose = "PROJECT_TEST_VERBOSE" - envTestCover = "PROJECT_TEST_COVER" - envBuildRebuildAll = "PROJECT_BUILD_REBUILD_ALL" - envBuildVerbose = "PROJECT_BUILD_VERBOSE" + + envInstallPrefix = "PROJECT_INSTALL_PREFIX" + envTestVerbose = "PROJECT_TEST_VERBOSE" + envTestCover = "PROJECT_TEST_COVER" + envBuildRebuildAll = "PROJECT_BUILD_REBUILD_ALL" + envBuildVerbose = "PROJECT_BUILD_VERBOSE" + envFailOnFormatting = "PROJECT_FAIL_ON_FORMATTING" ) var ( @@ -60,6 +63,33 @@ func Staticcheck() error { return sh.RunV("staticcheck", "./...") } +// Gofmt checks the code for formatting. +// To fail on formatting set PROJECT_FAIL_ON_FORMATTING=1 +func Gofmt() error { + output, err := sh.Output("go", "fmt", "./...") + if err != nil { + return err + } + + formattedFiles := "" + + for _, file := range strings.Split(output, "\n") { + formattedFiles += "\n- " + file + } + + if os.Getenv(envFailOnFormatting) != "1" { + fmt.Println(formattedFiles) + + return nil + } + + if len(output) != 0 { + return fmt.Errorf("The following files needed to be formatted: %s", formattedFiles) + } + + return nil +} + // Build build the executable. // To rebuild packages that are already up-to-date set PROJECT_BUILD_REBUILD_ALL=1 // To enable verbose mode set PROJECT_BUILD_VERBOSE=1