2023-08-11 14:59:21 +01:00
|
|
|
//go:build mage
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-08-11 18:33:26 +01:00
|
|
|
"runtime"
|
|
|
|
"time"
|
2023-08-11 14:59:21 +01:00
|
|
|
|
|
|
|
"github.com/magefile/mage/mg"
|
|
|
|
"github.com/magefile/mage/sh"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Default = Build
|
|
|
|
binary = "spruce"
|
|
|
|
defaultInstallPrefix = "/usr/local"
|
|
|
|
)
|
|
|
|
|
2023-12-06 14:42:18 +00:00
|
|
|
// Test run the go tests.
|
|
|
|
// To enable verbose mode set SPRUCE_TEST_VERBOSE=1.
|
|
|
|
// To enable coverage mode set SPRUCE_TEST_COVER=1.
|
|
|
|
func Test() error {
|
|
|
|
goTest := sh.RunCmd("go", "test")
|
|
|
|
|
|
|
|
args := []string{"./..."}
|
|
|
|
|
|
|
|
if os.Getenv("SPRUCE_TEST_VERBOSE") == "1" {
|
|
|
|
args = append(args, "-v")
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("SPRUCE_TEST_COVER") == "1" {
|
|
|
|
args = append(args, "-cover")
|
|
|
|
}
|
|
|
|
|
|
|
|
return goTest(args...)
|
|
|
|
}
|
|
|
|
|
2023-08-21 03:07:06 +01:00
|
|
|
// Lint runs golangci-lint against the code.
|
|
|
|
func Lint() error {
|
|
|
|
return sh.RunV("golangci-lint", "run", "--color", "always")
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:59:21 +01:00
|
|
|
// Build builds the binary.
|
|
|
|
func Build() error {
|
|
|
|
flags := ldflags()
|
2023-08-16 23:33:28 +01:00
|
|
|
return sh.Run("go", "build", "-ldflags="+flags, "-a", "-o", binary, "./cmd/spruce")
|
2023-08-11 14:59:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2023-08-12 14:08:50 +01:00
|
|
|
if err := sh.Copy(dest, binary); err != nil {
|
2023-08-11 14:59:21 +01:00
|
|
|
return fmt.Errorf("unable to install %s; %w", binary, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-17 21:51:53 +01:00
|
|
|
// Documentation generates AsciiDoc documentation.
|
|
|
|
func Documentation() error {
|
|
|
|
documentation, err := sh.Output("go", "run", "./cmd/spruce-docgen")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to produce documentation; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := "./docs/schema.asciidoc"
|
|
|
|
|
|
|
|
file, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create %s; %w", filename, err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
fmt.Fprint(file, documentation)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:59:21 +01:00
|
|
|
// Clean cleans the workspace
|
|
|
|
func Clean() error {
|
2023-08-12 09:43:45 +01:00
|
|
|
files := []string{binary, "cv.pdf", "cv.json"}
|
2023-08-11 19:31:26 +01:00
|
|
|
|
|
|
|
for i := range files {
|
|
|
|
if err := sh.Rm(files[i]); err != nil {
|
|
|
|
return fmt.Errorf("unable to remove %s; %w", binary, err)
|
|
|
|
}
|
2023-08-11 14:59:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-08-11 18:33:26 +01:00
|
|
|
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)
|
2023-08-11 14:59:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2023-08-11 18:33:26 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|