From ac724b934a62e3667e8fa9f639ea72b6b8befd87 Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Sat, 18 Sep 2021 14:17:29 +0200 Subject: executil: Verify that working directory exists --- executil/executil.go | 5 ++++- 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 { -- cgit v1.2.3