Add Mage build system

Set up magefiles/ directory with targets for test, coverage,
lint, and build commands. Uses github.com/magefile/mage v1.15.0.
This commit is contained in:
Oliver Jakoubek 2026-01-15 19:19:58 +01:00
commit b325e7b3fd
4 changed files with 73 additions and 1 deletions

63
magefiles/magefile.go Normal file
View file

@ -0,0 +1,63 @@
//go:build mage
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"github.com/magefile/mage/sh"
)
// Test runs all tests with race detection and coverage.
func Test() error {
fmt.Println("Running tests with race detection and coverage...")
return sh.RunV("go", "test", "-race", "-coverprofile=coverage.out", "./...")
}
// Coverage runs tests and opens the HTML coverage report in a browser.
func Coverage() error {
if err := Test(); err != nil {
return err
}
fmt.Println("Generating HTML coverage report...")
if err := sh.RunV("go", "tool", "cover", "-html=coverage.out", "-o", "coverage.html"); err != nil {
return err
}
fmt.Println("Opening coverage report...")
return openBrowser("coverage.html")
}
// Lint runs golangci-lint.
func Lint() error {
fmt.Println("Running golangci-lint...")
return sh.RunV("golangci-lint", "run", "./...")
}
// Build verifies the module compiles.
func Build() error {
fmt.Println("Verifying module compiles...")
return sh.RunV("go", "build", "./...")
}
// openBrowser opens the specified file in the default browser.
func openBrowser(file string) error {
var cmd *exec.Cmd
switch runtime.GOOS {
case "linux":
cmd = exec.Command("xdg-open", file)
case "darwin":
cmd = exec.Command("open", file)
case "windows":
cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", file)
default:
return fmt.Errorf("unsupported platform: %s", runtime.GOOS)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Start()
}