From 5d447923b18b01327adc7e8aec16027d63fd68d8 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 26 Aug 2024 10:30:14 +0100 Subject: [PATCH] Initial commit --- .forgejo/workflows/workflow.yaml | 37 +++++++++ .gitignore | 2 + .golangci.yaml | 22 +++++ __build/.gitkeep | 0 magefiles/mage.go | 137 +++++++++++++++++++++++++++++++ magefiles/main.go | 13 +++ main.go | 24 ++++++ 7 files changed, 235 insertions(+) create mode 100644 .forgejo/workflows/workflow.yaml create mode 100644 .gitignore create mode 100644 .golangci.yaml create mode 100644 __build/.gitkeep create mode 100644 magefiles/mage.go create mode 100644 magefiles/main.go create mode 100644 main.go diff --git a/.forgejo/workflows/workflow.yaml b/.forgejo/workflows/workflow.yaml new file mode 100644 index 0000000..e08ccb4 --- /dev/null +++ b/.forgejo/workflows/workflow.yaml @@ -0,0 +1,37 @@ +--- +on: + pull_request: + types: + - opened + - reopened + - synchronize + +jobs: + test: + 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: Setup Go + uses: https://code.forgejo.org/actions/setup-go@v5 + with: + go-version: '1.22' + - name: Test + run: go run magefiles/main.go -v test + + lint: + runs-on: docker + steps: + - name: Checkout Repository + uses: https://code.forgejo.org/actions/checkout@v4 + - name: Setup Go + uses: https://code.forgejo.org/actions/setup-go@v5 + with: + go-version: '1.22' + - name: Lint + uses: https://github.com/golangci/golangci-lint-action@v3 + with: + version: v1.54 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..8549273 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,22 @@ +--- +run: + concurrency: 2 + timeout: 1m + issues-exit-code: 1 + tests: true + +output: + format: 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 +}