enbas/internal/build/magefiles/mage.go

141 lines
3 KiB
Go
Raw Normal View History

2024-02-19 12:39:01 +00:00
//go:build mage
package main
import (
"fmt"
"os"
2024-02-25 15:10:02 +00:00
"path/filepath"
"runtime"
"time"
2024-02-19 12:39:01 +00:00
2024-02-25 15:10:02 +00:00
"github.com/magefile/mage/mg"
2024-02-19 12:39:01 +00:00
"github.com/magefile/mage/sh"
)
2024-02-25 15:10:02 +00:00
const (
binary = "enbas"
defaultInstallPrefix = "/usr/local"
envInstallPrefix = "ENBAS_INSTALL_PREFIX"
envTestVerbose = "ENBAS_TEST_VERBOSE"
envTestCover = "ENBAS_TEST_COVER"
)
2024-02-19 12:39:01 +00:00
2024-02-25 15:10:02 +00:00
var Default = Build
2024-02-19 12:39:01 +00:00
// Test run the go tests.
// To enable verbose mode set ENBAS_TEST_VERBOSE=1.
// To enable coverage mode set ENBAS_TEST_COVER=1.
func Test() error {
if err := changeToProjectRoot(); err != nil {
return fmt.Errorf("unable to change to the project's root directory; %w", err)
}
goTest := sh.RunCmd("go", "test")
args := []string{"./..."}
2024-02-25 15:10:02 +00:00
if os.Getenv(envTestVerbose) == "1" {
2024-02-19 12:39:01 +00:00
args = append(args, "-v")
}
2024-02-25 15:10:02 +00:00
if os.Getenv(envTestCover) == "1" {
2024-02-19 12:39:01 +00:00
args = append(args, "-cover")
}
return goTest(args...)
}
// Lint runs golangci-lint against the code.
func Lint() error {
if err := changeToProjectRoot(); err != nil {
return fmt.Errorf("unable to change to the project's root directory; %w", err)
}
return sh.RunV("golangci-lint", "run", "--color", "always")
}
// Build build the executable.
func Build() error {
if err := changeToProjectRoot(); err != nil {
return fmt.Errorf("unable to change to the project's root directory; %w", err)
}
flags := ldflags()
return sh.Run("go", "build", "-ldflags="+flags, "-a", "-o", binary, "./cmd/enbas")
2024-02-19 12:39:01 +00:00
}
2024-02-25 15:10:02 +00:00
// Install install the executable.
func Install() error {
mg.Deps(Build)
installPrefix := os.Getenv(envInstallPrefix)
if installPrefix == "" {
installPrefix = defaultInstallPrefix
}
dest := filepath.Join(installPrefix, "bin", binary)
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", binary, dest)
return nil
}
2024-02-19 12:39:01 +00:00
// Clean clean the workspace.
func Clean() error {
if err := changeToProjectRoot(); err != nil {
return fmt.Errorf("unable to change to the project's root directory; %w", err)
}
if err := sh.Rm(binary); err != nil {
return err
}
if err := sh.Run("go", "clean", "./..."); err != nil {
return err
}
return nil
}
func changeToProjectRoot() error {
if err := os.Chdir("../.."); err != nil {
return fmt.Errorf("unable to change directory; %w", 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
}