spruce/magefiles/mage.go
Dan Anglin 36acb1a324
feat: add create subcommand and FlagSet
- Add a new subcommand and FlagSet for creating new CV JSON files.
- fix: close the file after reading the CV.
2023-08-12 09:43:45 +01:00

88 lines
1.8 KiB
Go

//go:build mage
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"
"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, "-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 {
files := []string{binary, "cv.pdf", "cv.json"}
for i := range files {
if err := sh.Rm(files[i]); 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.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
}