check formatting in ci
Some checks failed
CI / Tests (pull_request) Failing after 9s
CI / Style (pull_request) Failing after 9s

This commit is contained in:
Dan Anglin 2024-10-13 12:52:44 +01:00
parent f393d982f5
commit d25c157b20
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
4 changed files with 47 additions and 14 deletions

View file

@ -3,6 +3,8 @@ name: CI
on: on:
pull_request: pull_request:
branches:
- main
types: types:
- opened - opened
- synchronize - synchronize
@ -15,14 +17,14 @@ jobs:
steps: steps:
- name: Checkout Repository - name: Checkout Repository
uses: https://code.forgejo.org/actions/checkout@v4 uses: https://code.forgejo.org/actions/checkout@v4
- name: Test - name: Run tests
uses: https://codeflow.dananglin.me.uk/actions/mage-ci@main uses: https://codeflow.dananglin.me.uk/actions/mage-ci@main
with: with:
target: test target: test
env: env:
PROJECT_TEST_VERBOSE: "1" PROJECT_TEST_VERBOSE: "1"
PROJECT_TEST_COVER: "1" PROJECT_TEST_COVER: "1"
- name: Gosec - name: Run gosec
uses: https://codeflow.dananglin.me.uk/actions/mage-ci@main uses: https://codeflow.dananglin.me.uk/actions/mage-ci@main
with: with:
target: gosec target: gosec
@ -34,6 +36,12 @@ jobs:
steps: steps:
- name: Checkout Repository - name: Checkout Repository
uses: https://code.forgejo.org/actions/checkout@v4 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 - name: Run staticcheck
uses: https://codeflow.dananglin.me.uk/actions/mage-ci@main uses: https://codeflow.dananglin.me.uk/actions/mage-ci@main
with: with:

View file

@ -18,9 +18,7 @@ func main() {
// Set up logging // Set up logging
loggingLevel := new(slog.LevelVar) loggingLevel := new(slog.LevelVar)
slogOpts := slog.HandlerOptions{ slogOpts := slog.HandlerOptions{Level: loggingLevel}
Level: loggingLevel,
}
logger := slog.New(slog.NewTextHandler(os.Stdout, &slogOpts)) logger := slog.New(slog.NewTextHandler(os.Stdout, &slogOpts))
slog.SetDefault(logger) slog.SetDefault(logger)

View file

@ -20,8 +20,5 @@ func newCommand(args []string) command {
} }
} }
return command{ return command{name: args[0], args: args[1:]}
name: args[0],
args: args[1:],
}
} }

View file

@ -7,6 +7,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings"
"time" "time"
"github.com/magefile/mage/mg" "github.com/magefile/mage/mg"
@ -16,11 +17,13 @@ import (
const ( const (
app = "indieauth-server" app = "indieauth-server"
defaultInstallPrefix = "/usr/local" defaultInstallPrefix = "/usr/local"
envInstallPrefix = "PROJECT_INSTALL_PREFIX"
envTestVerbose = "PROJECT_TEST_VERBOSE" envInstallPrefix = "PROJECT_INSTALL_PREFIX"
envTestCover = "PROJECT_TEST_COVER" envTestVerbose = "PROJECT_TEST_VERBOSE"
envBuildRebuildAll = "PROJECT_BUILD_REBUILD_ALL" envTestCover = "PROJECT_TEST_COVER"
envBuildVerbose = "PROJECT_BUILD_VERBOSE" envBuildRebuildAll = "PROJECT_BUILD_REBUILD_ALL"
envBuildVerbose = "PROJECT_BUILD_VERBOSE"
envFailOnFormatting = "PROJECT_FAIL_ON_FORMATTING"
) )
var ( var (
@ -60,6 +63,33 @@ func Staticcheck() error {
return sh.RunV("staticcheck", "./...") 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. // Build build the executable.
// To rebuild packages that are already up-to-date set PROJECT_BUILD_REBUILD_ALL=1 // To rebuild packages that are already up-to-date set PROJECT_BUILD_REBUILD_ALL=1
// To enable verbose mode set PROJECT_BUILD_VERBOSE=1 // To enable verbose mode set PROJECT_BUILD_VERBOSE=1