diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-20 22:56:07 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-20 22:56:07 +0200 |
| commit | eb3da18b530f8049212480e0be47fa8e00f97c73 (patch) | |
| tree | d670d6f56a370647a9ab48996016f0a3844dfd00 | |
| parent | 591f12f6c2eafa0d565ed85867d03e92f28406b5 (diff) | |
internal/Magefile: Refactor to align with best practices
- Added mg import (via magefile/mage/sh)
- Added binaryName constant
- Added project descriptions for build targets
- Added proper Install target with go install
- Added Uninstall target to remove the binary
- Added getGOPATH helper function
| -rw-r--r-- | Magefile.go | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/Magefile.go b/Magefile.go index 1664710..cfc9911 100644 --- a/Magefile.go +++ b/Magefile.go @@ -3,31 +3,65 @@ package main import ( + "fmt" + "os" + "path/filepath" + "github.com/magefile/mage/sh" ) +// Project description for build output +const binaryName = "perc" + +// Default is the default target when no target is specified. var Default = Build +// Build builds the perc binary. func Build() error { - return sh.RunV("go", "build", "-o", "perc", "./cmd/perc") + fmt.Println("Building perc...") + return sh.RunV("go", "build", "-o", binaryName, "./cmd/perc") } +// Run runs the perc binary. func Run() error { return sh.RunV("go", "run", "./cmd/perc") } +// Test runs all tests. func Test() error { + fmt.Println("Running all tests...") return sh.RunV("go", "test", "./...") } +// TestRPN runs tests for the RPN package. func TestRPN() error { + fmt.Println("Running RPN tests...") return sh.RunV("go", "test", "./internal/rpn/...") } +// Install installs the perc binary to GOPATH/bin. func Install() error { + fmt.Println("Installing perc...") return sh.RunV("go", "install", "./cmd/perc") } +// Repl starts the REPL mode. func Repl() error { return sh.RunV("go", "run", "./cmd/perc", "--repl") } + +// Uninstall removes the perc binary from GOPATH/bin. +func Uninstall() error { + fmt.Println("Uninstalling perc...") + binPath := filepath.Join(getGOPATH(), "bin", binaryName) + return os.Remove(binPath) +} + +// getGOPATH returns the GOPATH environment variable. +func getGOPATH() string { + gopath := os.Getenv("GOPATH") + if gopath == "" { + gopath = filepath.Join(os.Getenv("HOME"), "go") + } + return gopath +} |
