generated from templates/go-generic
check formatting in ci
This commit is contained in:
parent
f393d982f5
commit
d25c157b20
4 changed files with 47 additions and 14 deletions
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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:]}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue