From 8c5e7e89cd394b02406afedf25aa41a23cd16570 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 12 Mar 2017 17:18:59 -0300 Subject: [PATCH] Little refactor on command creation --- execext/exec.go | 22 ++++++++++++++++++++++ execext/exec_other.go | 13 +++++++++++++ execext/exec_win.go | 20 ++++++++++++++++++++ task.go | 23 +++-------------------- variable_handling.go | 12 ++++-------- 5 files changed, 62 insertions(+), 28 deletions(-) create mode 100644 execext/exec.go create mode 100644 execext/exec_other.go create mode 100644 execext/exec_win.go diff --git a/execext/exec.go b/execext/exec.go new file mode 100644 index 00000000..b751da34 --- /dev/null +++ b/execext/exec.go @@ -0,0 +1,22 @@ +package execext + +import ( + "os/exec" +) + +var ( + // ShPath is path to "sh" command + ShPath string + // ShExists is true if "sh" command is available on the system + ShExists bool +) + +func init() { + var err error + ShPath, err = exec.LookPath("sh") + ShExists = err == nil +} + +func newShCommand(c string) *exec.Cmd { + return exec.Command(ShPath, "-c", c) +} diff --git a/execext/exec_other.go b/execext/exec_other.go new file mode 100644 index 00000000..e8f5410d --- /dev/null +++ b/execext/exec_other.go @@ -0,0 +1,13 @@ +// +build !windows + +package execext + +import ( + "os/exec" +) + +// NewCommand returns a new command that runs on "sh" is available or on "cmd" +// otherwise on Windows +func NewCommand(c string) *exec.Cmd { + return newShCommand(c) +} diff --git a/execext/exec_win.go b/execext/exec_win.go new file mode 100644 index 00000000..3054dcda --- /dev/null +++ b/execext/exec_win.go @@ -0,0 +1,20 @@ +// +build windows + +package execext + +import ( + "os/exec" +) + +// NewCommand returns a new command that runs on "sh" is available or on "cmd" +// otherwise on Windows +func NewCommand(c string) *exec.Cmd { + if ShExists { + return newShCommand(c) + } + return newCmdCommand(c) +} + +func newCmdCommand(c string) { + return exec.Command("cmd", "/C", c) +} diff --git a/task.go b/task.go index f0c14b87..974b7472 100644 --- a/task.go +++ b/task.go @@ -4,19 +4,16 @@ import ( "fmt" "log" "os" - "os/exec" "strings" + "github.com/go-task/task/execext" + "github.com/spf13/pflag" ) var ( // TaskFilePath is the default Taskfile TaskFilePath = "Taskfile" - // ShExists is true if Bash was found - ShExists bool - // ShPath constains the Bash path if found - ShPath string // Force (--force or -f flag) forces a task to run even when it's up-to-date Force bool @@ -27,15 +24,6 @@ var ( runnedTasks = make(map[string]struct{}) ) -func init() { - var err error - ShPath, err = exec.LookPath("sh") - if err != nil { - return - } - ShExists = true -} - // Task represents a task type Task struct { Cmds []string @@ -142,12 +130,7 @@ func (t *Task) runCommand(i int) error { if err != nil { return err } - var cmd *exec.Cmd - if ShExists { - cmd = exec.Command(ShPath, "-c", c) - } else { - cmd = exec.Command("cmd", "/C", c) - } + cmd := execext.NewCommand(c) if dir != "" { cmd.Dir = dir } diff --git a/variable_handling.go b/variable_handling.go index 484c68f2..976938c6 100644 --- a/variable_handling.go +++ b/variable_handling.go @@ -6,11 +6,12 @@ import ( "errors" "io/ioutil" "os" - "os/exec" "runtime" "strings" "text/template" + "github.com/go-task/task/execext" + "github.com/BurntSushi/toml" "gopkg.in/yaml.v2" ) @@ -31,12 +32,7 @@ func handleDynamicVariableContent(value string) (string, error) { if result, ok := varCmds[value]; ok { return result, nil } - var cmd *exec.Cmd - if ShExists { - cmd = exec.Command(ShPath, "-c", value[1:]) - } else { - cmd = exec.Command("cmd", "/C", value[1:]) - } + cmd := execext.NewCommand(value[1:]) cmd.Stdin = os.Stdin cmd.Stderr = os.Stderr b, err := cmd.Output() @@ -83,7 +79,7 @@ func (t *Task) handleVariables() (map[string]string, error) { var templateFuncs = template.FuncMap{ "OS": func() string { return runtime.GOOS }, "ARCH": func() string { return runtime.GOARCH }, - "IsSH": func() bool { return ShExists }, + "IsSH": func() bool { return execext.ShExists }, } // ReplaceVariables writes vars into initial string