summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-06-25 11:46:20 +0300
committerPaul Buetow <paul@buetow.org>2026-06-25 11:46:20 +0300
commit37f0670249aedd92d573454f156618138c57029e (patch)
tree406de6075df546dc9dfa00f69b4c67ac1278ab86
parent9c96b88d6f1f9c92b4bca20fac59a9be56f511a2 (diff)
Fix Magefile target semantics for 1r0
-rw-r--r--Magefile.go45
-rw-r--r--Magefile_test.go40
2 files changed, 60 insertions, 25 deletions
diff --git a/Magefile.go b/Magefile.go
index 9a1430b..6782f90 100644
--- a/Magefile.go
+++ b/Magefile.go
@@ -16,10 +16,8 @@ import (
const binaryName = "tasksamurai"
-// Default builds the tasksamurai binary.
-func Default() {
- mg.Deps(Build)
-}
+// Default is the target Mage runs when no target is specified.
+var Default = Build
// Build compiles the tasksamurai binary.
func Build() error {
@@ -30,12 +28,12 @@ func Build() error {
return nil
}
-// Run builds and starts tasksamurai with any provided arguments.
+// Run builds and starts tasksamurai.
func Run() error {
mg.Deps(Build)
fmt.Println("Running tasksamurai...")
- if err := sh.RunV("./"+binaryName, targetArgs("run")...); err != nil {
+ if err := sh.RunV("./" + binaryName); err != nil {
return fmt.Errorf("run %s: %w", binaryName, err)
}
return nil
@@ -75,16 +73,10 @@ func Install() error {
mg.Deps(Build)
fmt.Println("Installing tasksamurai...")
- goPath := os.Getenv("GOPATH")
- if goPath == "" {
- home, err := os.UserHomeDir()
- if err != nil {
- return fmt.Errorf("resolve home directory: %w", err)
- }
- goPath = filepath.Join(home, "go")
+ binDir, err := installBinDir()
+ if err != nil {
+ return err
}
-
- binDir := filepath.Join(goPath, "bin")
if err := os.MkdirAll(binDir, 0o755); err != nil {
return fmt.Errorf("create install directory %s: %w", binDir, err)
}
@@ -107,17 +99,20 @@ func Clean() error {
return nil
}
-func targetArgs(target string) []string {
- for i, arg := range os.Args[1:] {
- if !strings.EqualFold(arg, target) {
- continue
+func installBinDir() (string, error) {
+ goPath := os.Getenv("GOPATH")
+ if goPath == "" {
+ home, err := os.UserHomeDir()
+ if err != nil {
+ return "", fmt.Errorf("resolve home directory: %w", err)
}
-
- args := os.Args[i+2:]
- if len(args) > 0 && args[0] == "--" {
- return args[1:]
+ goPath = filepath.Join(home, "go")
+ } else {
+ goPath = filepath.SplitList(goPath)[0]
+ if goPath == "" {
+ return "", fmt.Errorf("resolve GOPATH: first path entry is empty")
}
- return args
}
- return nil
+
+ return filepath.Join(goPath, "bin"), nil
}
diff --git a/Magefile_test.go b/Magefile_test.go
new file mode 100644
index 0000000..fa29ab1
--- /dev/null
+++ b/Magefile_test.go
@@ -0,0 +1,40 @@
+//go:build mage
+// +build mage
+
+package main
+
+import (
+ "path/filepath"
+ "testing"
+)
+
+func TestInstallBinDirUsesSingleGOPATH(t *testing.T) {
+ goPath := t.TempDir()
+ t.Setenv("GOPATH", goPath)
+
+ got, err := installBinDir()
+ if err != nil {
+ t.Fatalf("installBinDir() error = %v", err)
+ }
+
+ want := filepath.Join(goPath, "bin")
+ if got != want {
+ t.Fatalf("installBinDir() = %q, want %q", got, want)
+ }
+}
+
+func TestInstallBinDirUsesFirstGOPATHEntry(t *testing.T) {
+ first := t.TempDir()
+ second := t.TempDir()
+ t.Setenv("GOPATH", first+string(filepath.ListSeparator)+second)
+
+ got, err := installBinDir()
+ if err != nil {
+ t.Fatalf("installBinDir() error = %v", err)
+ }
+
+ want := filepath.Join(first, "bin")
+ if got != want {
+ t.Fatalf("installBinDir() = %q, want %q", got, want)
+ }
+}