mirror of
https://github.com/go-task/task.git
synced 2025-02-03 13:22:11 +02:00
Little refactor on command creation
This commit is contained in:
parent
678ea86350
commit
8c5e7e89cd
22
execext/exec.go
Normal file
22
execext/exec.go
Normal file
@ -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)
|
||||
}
|
13
execext/exec_other.go
Normal file
13
execext/exec_other.go
Normal file
@ -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)
|
||||
}
|
20
execext/exec_win.go
Normal file
20
execext/exec_win.go
Normal file
@ -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)
|
||||
}
|
23
task.go
23
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
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user