commit c29412e612468f14c0cad80c1911d3384749b79b Author: Apollo <> Date: Wed Aug 28 12:36:14 2024 +0100 Initial commit diff --git a/.forgejo/workflows/workflow.yaml b/.forgejo/workflows/workflow.yaml new file mode 100644 index 0000000..13935dc --- /dev/null +++ b/.forgejo/workflows/workflow.yaml @@ -0,0 +1,26 @@ +--- +name: Workflow + +on: + pull_request: + types: + - opened + - synchronize + +jobs: + test: + if: ${{ ! github.event.pull_request.draft }} + runs-on: docker + env: + GO_TEST_VERBOSE: "1" + GO_TEST_COVER: "1" + steps: + - name: Checkout Repository + uses: https://code.forgejo.org/actions/checkout@v4 + - name: Test + uses: https://codeflow.dananglin.me.uk/actions/mage-ci@main + with: + target: test + env: + PROJECT_TEST_VERBOSE: "1" + PROJECT_TEST_COVER: "1" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e200850 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/__build/* +!__build/.gitkeep diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..059f5bc --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,22 @@ +--- +run: + concurrency: 2 + timeout: 1m + issues-exit-code: 1 + tests: true + +output: + formats: colored-line-number + print-issues-lines: true + print-linter-name: true + uniq-by-line: true + sort-results: true + +linters-settings: + lll: + line-length: 140 + +linters: + enable-all: true + # disable: + fast: false diff --git a/__build/.gitkeep b/__build/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/magefiles/mage.go b/magefiles/mage.go new file mode 100644 index 0000000..1a12267 --- /dev/null +++ b/magefiles/mage.go @@ -0,0 +1,137 @@ +//go:build mage + +package main + +import ( + "fmt" + "os" + "path/filepath" + "runtime" + "time" + + "github.com/magefile/mage/mg" + "github.com/magefile/mage/sh" +) + +const ( + app = "binary" + defaultInstallPrefix = "/usr/local" + envInstallPrefix = "PROJECT_INSTALL_PREFIX" + envTestVerbose = "PROJECT_TEST_VERBOSE" + envTestCover = "PROJECT_TEST_COVER" + envBuildRebuildAll = "PROJECT_BUILD_REBUILD_ALL" + envBuildVerbose = "PROJECT_BUILD_VERBOSE" +) + +var ( + Default = Build + binary = "./__build/" + app +) + +// Test run the go tests. +// To enable verbose mode set PROJECT_TEST_VERBOSE=1. +// To enable coverage mode set PROJECT_TEST_COVER=1. +func Test() error { + goTest := sh.RunCmd("go", "test") + + args := []string{"./..."} + + if os.Getenv(envTestVerbose) == "1" { + args = append(args, "-v") + } + + if os.Getenv(envTestCover) == "1" { + args = append(args, "-cover") + } + + return goTest(args...) +} + +// Lint runs golangci-lint against the code. +func Lint() error { + return sh.RunV("golangci-lint", "run", "--color", "always") +} + +// 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 +func Build() error { + main := "main.go" + flags := ldflags() + build := sh.RunCmd("go", "build") + args := []string{"-ldflags=" + flags, "-o", binary} + + if os.Getenv(envBuildRebuildAll) == "1" { + args = append(args, "-a") + } + + if os.Getenv(envBuildVerbose) == "1" { + args = append(args, "-v") + } + + args = append(args, main) + + return build(args...) +} + +// Install install the executable. +func Install() error { + mg.Deps(Build) + + installPrefix := os.Getenv(envInstallPrefix) + + if installPrefix == "" { + installPrefix = defaultInstallPrefix + } + + dest := filepath.Join(installPrefix, "bin", app) + + if err := sh.Copy(dest, binary); err != nil { + return fmt.Errorf("unable to install %s; %w", dest, err) + } + + fmt.Printf("%s successfully installed to %s\n", app, dest) + + return nil +} + +// Clean clean the workspace. +func Clean() error { + if err := sh.Rm(binary); err != nil { + return err + } + + if err := sh.Run("go", "clean", "./..."); err != nil { + return err + } + + return nil +} + +// ldflags returns the build flags. +func ldflags() string { + ldflagsfmt := "-s -w -X main.binaryVersion=%s -X main.gitCommit=%s -X main.goVersion=%s -X main.buildTime=%s" + buildTime := time.Now().UTC().Format(time.RFC3339) + + return fmt.Sprintf(ldflagsfmt, version(), gitCommit(), runtime.Version(), buildTime) +} + +// version returns the latest git tag using git describe. +func version() string { + version, err := sh.Output("git", "describe", "--tags") + if err != nil { + version = "N/A" + } + + return version +} + +// gitCommit returns the current git commit +func gitCommit() string { + commit, err := sh.Output("git", "rev-parse", "--short", "HEAD") + if err != nil { + commit = "N/A" + } + + return commit +} diff --git a/magefiles/main.go b/magefiles/main.go new file mode 100644 index 0000000..8883df9 --- /dev/null +++ b/magefiles/main.go @@ -0,0 +1,13 @@ +//go:build ignore + +package main + +import ( + "os" + + "github.com/magefile/mage/mage" +) + +func main() { + os.Exit(mage.Main()) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..a3255e8 --- /dev/null +++ b/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "os" +) + +var ( + binaryVersion string + buildTime string + goVersion string + gitCommit string +) + +func main() { + if err := run(); err != nil { + fmt.Printf("ERROR: %v.\n", err) + os.Exit(1) + } +} + +func run() error { + return nil +}