Skip to content

Commit

Permalink
Set magefiles working directory (#277)
Browse files Browse the repository at this point in the history
* Use -w as working directory flag
* Make Invocation.WorkDir works as expected when mage is used as library
* Add test for setting working directory
* Sort CLI flags in the documentation
  • Loading branch information
ntrrg authored and natefinch committed Jan 17, 2020
1 parent 0c7e3de commit 5d10b67
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 11 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ mage_output_file.go
# Goland
.idea
*.iml

# Vim
*.sw[op]
Session.vim
*~

# GNU Screen
.screenrc
22 changes: 16 additions & 6 deletions mage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func Main() int {
type Invocation struct {
Debug bool // turn on debug messages
Dir string // directory to read magefiles from
WorkDir string // directory where magefiles will run
Force bool // forces recreation of the compiled binary
Verbose bool // tells the magefile to print out log statements
List bool // tells the magefile to print out a list of targets
Expand Down Expand Up @@ -176,7 +177,8 @@ func Parse(stderr, stdout io.Writer, args []string) (inv Invocation, cmd Command
fs.BoolVar(&inv.Help, "h", false, "show this help")
fs.DurationVar(&inv.Timeout, "t", 0, "timeout in duration parsable format (e.g. 5m30s)")
fs.BoolVar(&inv.Keep, "keep", false, "keep intermediate mage files around after running")
fs.StringVar(&inv.Dir, "d", ".", "run magefiles in the given directory")
fs.StringVar(&inv.Dir, "d", ".", "directory to read magefiles from")
fs.StringVar(&inv.WorkDir, "w", inv.Dir, "working directory where magefiles will run")
fs.StringVar(&inv.GoCmd, "gocmd", mg.GoCmd(), "use the given go binary to compile the output")
fs.StringVar(&inv.GOOS, "goos", "", "set GOOS for binary produced with -compile")
fs.StringVar(&inv.GOARCH, "goarch", "", "set GOARCH for binary produced with -compile")
Expand All @@ -203,25 +205,27 @@ Commands:
-clean clean out old generated binaries from CACHE_DIR
-compile <string>
output a static binary to the given path
-h show this help
-init create a starting template if no mage files exist
-l list mage targets in this directory
-h show this help
-version show version info for the mage binary
Options:
-d <string>
run magefiles in the given directory (default ".")
directory to read magefiles from (default ".")
-debug turn on debug messages
-h show description of a target
-f force recreation of compiled magefile
-keep keep intermediate mage files around after running
-goarch sets the GOARCH for the binary created by -compile (default: current arch)
-gocmd <string>
use the given go binary to compile the output (default: "go")
-goos sets the GOOS for the binary created by -compile (default: current OS)
-goarch sets the GOARCH for the binary created by -compile (default: current arch)
-h show description of a target
-keep keep intermediate mage files around after running
-t <string>
timeout in duration parsable format (e.g. 5m30s)
-v show verbose output when running mage targets
-w <string>
working directory where magefiles will run (default -d value)
`[1:])
}
err = fs.Parse(args)
Expand Down Expand Up @@ -297,6 +301,9 @@ func Invoke(inv Invocation) int {
if inv.Dir == "" {
inv.Dir = "."
}
if inv.WorkDir == "" {
inv.WorkDir = inv.Dir
}
if inv.CacheDir == "" {
inv.CacheDir = mg.CacheDir()
}
Expand Down Expand Up @@ -617,6 +624,9 @@ func RunCompiled(inv Invocation, exePath string, errlog *log.Logger) int {
c.Stdout = inv.Stdout
c.Stdin = inv.Stdin
c.Dir = inv.Dir
if inv.WorkDir != inv.Dir {
c.Dir = inv.WorkDir
}
// intentionally pass through unaltered os.Environ here.. your magefile has
// to deal with it.
c.Env = os.Environ()
Expand Down
24 changes: 24 additions & 0 deletions mage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,30 @@ func TestSetDir(t *testing.T) {
}
}

func TestSetWorkingDir(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
code := Invoke(Invocation{
Dir: "testdata/setworkdir",
WorkDir: "testdata/setworkdir/data",
Stdout: stdout,
Stderr: stderr,
Args: []string{"TestWorkingDir"},
})

if code != 0 {
t.Errorf(
"expected code 0, but got %d. Stdout:\n%s\nStderr:\n%s",
code, stdout, stderr,
)
}

expected := "file1.txt, file2.txt\n"
if out := stdout.String(); out != expected {
t.Fatalf("expected list of files to be %q, but was %q", expected, out)
}
}

// Test the timeout option
func TestTimeout(t *testing.T) {
stderr := &bytes.Buffer{}
Expand Down
1 change: 1 addition & 0 deletions mage/testdata/setworkdir/data/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lorem ipsum 1
1 change: 1 addition & 0 deletions mage/testdata/setworkdir/data/file2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lorem ipsum 2
23 changes: 23 additions & 0 deletions mage/testdata/setworkdir/magefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//+build mage

package main

import (
"fmt"
"io/ioutil"
"strings"
)

func TestWorkingDir() error {
files, err := ioutil.ReadDir(".")
if err != nil {
return err
}
var out []string
for _, f := range files {
out = append(out, f.Name())
}

fmt.Println(strings.Join(out, ", "))
return nil
}
12 changes: 7 additions & 5 deletions site/content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,27 @@ Commands:
-clean clean out old generated binaries from CACHE_DIR
-compile <string>
output a static binary to the given path
-h show this help
-init create a starting template if no mage files exist
-l list mage targets in this directory
-h show this help
-version show version info for the mage binary
Options:
-d <string>
run magefiles in the given directory (default ".")
directory to read magefiles from (default ".")
-debug turn on debug messages
-h show description of a target
-f force recreation of compiled magefile
-keep keep intermediate mage files around after running
-goarch sets the GOARCH for the binary created by -compile (default: current arch)
-gocmd <string>
use the given go binary to compile the output (default: "go")
-goos sets the GOOS for the binary created by -compile (default: current OS)
-goarch sets the GOARCH for the binary created by -compile (default: current arch)
-h show description of a target
-keep keep intermediate mage files around after running
-t <string>
timeout in duration parsable format (e.g. 5m30s)
-v show verbose output when running mage targets
-w <string>
working directory where magefiles will run (default -d value)
```

## Why?
Expand Down

0 comments on commit 5d10b67

Please sign in to comment.