diff options
author | Martin Polden <mpolden@mpolden.no> | 2021-09-18 14:17:29 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2021-09-18 14:19:20 +0200 |
commit | ac724b934a62e3667e8fa9f639ea72b6b8befd87 (patch) | |
tree | c8db7581015463184d4847a8ea993a2b91014933 | |
parent | f501a8c6ec4538ff92e2e4af6824ec9b20131822 (diff) |
executil: Verify that working directory exists
-rw-r--r-- | executil/executil.go | 5 | ||||
-rw-r--r-- | executil/executil_test.go | 6 |
2 files changed, 8 insertions, 3 deletions
diff --git a/executil/executil.go b/executil/executil.go index 0617910..1ef20c7 100644 --- a/executil/executil.go +++ b/executil/executil.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "html/template" + "os" "os/exec" "strings" ) @@ -28,7 +29,9 @@ func compileCommand(tmpl string, data CommandData) (*exec.Cmd, error) { return nil, fmt.Errorf("template compiled to empty command") } cmd := exec.Command(argv[0], argv[1:]...) - cmd.Dir = data.Dir + if _, err := os.Stat(data.Dir); err == nil { + cmd.Dir = data.Dir + } return cmd, nil } diff --git a/executil/executil_test.go b/executil/executil_test.go index f2633ab..d3c01f2 100644 --- a/executil/executil_test.go +++ b/executil/executil_test.go @@ -2,16 +2,18 @@ package executil import ( "os" + "path/filepath" "strings" "testing" ) func TestCompileCommand(t *testing.T) { tmpl := "tar -xf {{.Name}} {{.Base}} {{.Dir}}" + dir := t.TempDir() data := CommandData{ - Name: "/foo/bar/baz.rar", + Name: filepath.Join(dir, "baz.rar"), Base: "baz.rar", - Dir: "/foo/bar", + Dir: dir, } cmd, err := compileCommand(tmpl, data) if err != nil { |