build: add the install mage target

This commit is contained in:
Dan Anglin 2024-02-25 15:10:02 +00:00
parent 41d434a69e
commit 2177063ca4
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638

View file

@ -5,15 +5,23 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"runtime" "runtime"
"time" "time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh" "github.com/magefile/mage/sh"
) )
var Default = Build const (
binary = "enbas"
defaultInstallPrefix = "/usr/local"
envInstallPrefix = "ENBAS_INSTALL_PREFIX"
envTestVerbose = "ENBAS_TEST_VERBOSE"
envTestCover = "ENBAS_TEST_COVER"
)
var binary = "enbas" var Default = Build
// Test run the go tests. // Test run the go tests.
// To enable verbose mode set ENBAS_TEST_VERBOSE=1. // To enable verbose mode set ENBAS_TEST_VERBOSE=1.
@ -27,11 +35,11 @@ func Test() error {
args := []string{"./..."} args := []string{"./..."}
if os.Getenv("ENBAS_TEST_VERBOSE") == "1" { if os.Getenv(envTestVerbose) == "1" {
args = append(args, "-v") args = append(args, "-v")
} }
if os.Getenv("ENBAS_TEST_COVER") == "1" { if os.Getenv(envTestCover) == "1" {
args = append(args, "-cover") args = append(args, "-cover")
} }
@ -57,6 +65,27 @@ func Build() error {
return sh.Run("go", "build", "-ldflags="+flags, "-a", "-o", binary, "./cmd/enbas") return sh.Run("go", "build", "-ldflags="+flags, "-a", "-o", binary, "./cmd/enbas")
} }
// 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
}
// Clean clean the workspace. // Clean clean the workspace.
func Clean() error { func Clean() error {
if err := changeToProjectRoot(); err != nil { if err := changeToProjectRoot(); err != nil {