spruce/magefiles/mage.go
Dan Anglin 69c3165fc1
build: replace make with mage
- Replace the makefile with the magefile
- Update the Dockerfile
- Update go.mod
2023-08-11 14:59:21 +01:00

71 lines
1.4 KiB
Go

//go:build mage
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var (
Default = Build
binary = "spruce"
defaultInstallPrefix = "/usr/local"
)
// Build builds the binary.
func Build() error {
flags := ldflags()
return sh.Run("go", "build", "-ldflags="+flags, "-v", "-a", "-o", binary, ".")
}
// Install installs the binary to the execution path.
func Install() error {
mg.Deps(Build)
installPrefix := os.Getenv("SPRUCE_INSTALL_PREFIX")
if installPrefix == "" {
installPrefix = defaultInstallPrefix
}
dest := filepath.Join(installPrefix, "bin", binary)
if err := sh.Copy(binary, dest); err != nil {
return fmt.Errorf("unable to install %s; %w", binary, err)
}
return nil
}
// Clean cleans the workspace
func Clean() error {
if err := sh.Rm(binary); err != nil {
return fmt.Errorf("unable to remove %s; %w", binary, err)
}
if err := sh.Run("go", "clean", "./..."); err != nil {
return fmt.Errorf("unable to run 'go clean'; %w", err)
}
return nil
}
// ldflags returns the build flags.
func ldflags() string {
ldflagsfmt := "-s -w -X main.version=%s"
return fmt.Sprintf(ldflagsfmt, version())
}
// 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
}