Initial commit

This commit is contained in:
Apollo 2024-08-28 12:36:14 +01:00
commit c29412e612
7 changed files with 224 additions and 0 deletions

View file

@ -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"

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/__build/*
!__build/.gitkeep

22
.golangci.yaml Normal file
View file

@ -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

0
__build/.gitkeep Normal file
View file

137
magefiles/mage.go Normal file
View file

@ -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
}

13
magefiles/main.go Normal file
View file

@ -0,0 +1,13 @@
//go:build ignore
package main
import (
"os"
"github.com/magefile/mage/mage"
)
func main() {
os.Exit(mage.Main())
}

24
main.go Normal file
View file

@ -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
}