mirror of
https://github.com/go-task/task.git
synced 2025-06-25 00:47:04 +02:00
Little refactor on command creation
This commit is contained in:
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"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-task/task/execext"
|
||||||
|
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// TaskFilePath is the default Taskfile
|
// TaskFilePath is the default Taskfile
|
||||||
TaskFilePath = "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 (--force or -f flag) forces a task to run even when it's up-to-date
|
||||||
Force bool
|
Force bool
|
||||||
@ -27,15 +24,6 @@ var (
|
|||||||
runnedTasks = make(map[string]struct{})
|
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
|
// Task represents a task
|
||||||
type Task struct {
|
type Task struct {
|
||||||
Cmds []string
|
Cmds []string
|
||||||
@ -142,12 +130,7 @@ func (t *Task) runCommand(i int) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var cmd *exec.Cmd
|
cmd := execext.NewCommand(c)
|
||||||
if ShExists {
|
|
||||||
cmd = exec.Command(ShPath, "-c", c)
|
|
||||||
} else {
|
|
||||||
cmd = exec.Command("cmd", "/C", c)
|
|
||||||
}
|
|
||||||
if dir != "" {
|
if dir != "" {
|
||||||
cmd.Dir = dir
|
cmd.Dir = dir
|
||||||
}
|
}
|
||||||
|
@ -6,11 +6,12 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/go-task/task/execext"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
@ -31,12 +32,7 @@ func handleDynamicVariableContent(value string) (string, error) {
|
|||||||
if result, ok := varCmds[value]; ok {
|
if result, ok := varCmds[value]; ok {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
var cmd *exec.Cmd
|
cmd := execext.NewCommand(value[1:])
|
||||||
if ShExists {
|
|
||||||
cmd = exec.Command(ShPath, "-c", value[1:])
|
|
||||||
} else {
|
|
||||||
cmd = exec.Command("cmd", "/C", value[1:])
|
|
||||||
}
|
|
||||||
cmd.Stdin = os.Stdin
|
cmd.Stdin = os.Stdin
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
b, err := cmd.Output()
|
b, err := cmd.Output()
|
||||||
@ -83,7 +79,7 @@ func (t *Task) handleVariables() (map[string]string, error) {
|
|||||||
var templateFuncs = template.FuncMap{
|
var templateFuncs = template.FuncMap{
|
||||||
"OS": func() string { return runtime.GOOS },
|
"OS": func() string { return runtime.GOOS },
|
||||||
"ARCH": func() string { return runtime.GOARCH },
|
"ARCH": func() string { return runtime.GOARCH },
|
||||||
"IsSH": func() bool { return ShExists },
|
"IsSH": func() bool { return execext.ShExists },
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReplaceVariables writes vars into initial string
|
// ReplaceVariables writes vars into initial string
|
||||||
|
Reference in New Issue
Block a user