mirror of
https://github.com/go-task/task.git
synced 2025-01-22 05:10:17 +02:00
Refactoring and redirect Stdout and Stdin of not set var
This commit is contained in:
parent
4127ea5afe
commit
3aaf1b2ec1
49
task.go
49
task.go
@ -2,7 +2,6 @@ package task
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -100,24 +99,10 @@ func RunTask(name string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range t.Cmds {
|
for i := range t.Cmds {
|
||||||
// read in a each time, as a command could change a variable or it has been changed by a dependency
|
if err = t.runCommand(i); err != nil {
|
||||||
vars, err = t.handleVariables()
|
|
||||||
if err != nil {
|
|
||||||
return &taskRunError{name, err}
|
return &taskRunError{name, err}
|
||||||
}
|
}
|
||||||
var (
|
|
||||||
output string
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
if output, err = runCommand(ReplaceVariables(c, vars), ReplaceVariables(t.Dir, vars)); err != nil {
|
|
||||||
return &taskRunError{name, err}
|
|
||||||
}
|
|
||||||
if t.Set != "" {
|
|
||||||
os.Setenv(t.Set, output)
|
|
||||||
} else {
|
|
||||||
fmt.Println(output)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -140,25 +125,39 @@ func isTaskUpToDate(t *Task) bool {
|
|||||||
return generatesMinTime.After(sourcesMaxTime)
|
return generatesMinTime.After(sourcesMaxTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runCommand(c, path string) (string, error) {
|
func (t *Task) runCommand(i int) error {
|
||||||
|
vars, err := t.handleVariables()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
var (
|
var (
|
||||||
|
c = ReplaceVariables(t.Cmds[i], vars)
|
||||||
|
dir = ReplaceVariables(t.Dir, vars)
|
||||||
cmd *exec.Cmd
|
cmd *exec.Cmd
|
||||||
b []byte
|
|
||||||
err error
|
|
||||||
)
|
)
|
||||||
if ShExists {
|
if ShExists {
|
||||||
cmd = exec.Command(ShPath, "-c", c)
|
cmd = exec.Command(ShPath, "-c", c)
|
||||||
} else {
|
} else {
|
||||||
cmd = exec.Command("cmd", "/C", c)
|
cmd = exec.Command("cmd", "/C", c)
|
||||||
}
|
}
|
||||||
if path != "" {
|
if dir != "" {
|
||||||
cmd.Dir = path
|
cmd.Dir = dir
|
||||||
}
|
}
|
||||||
|
cmd.Stdin = os.Stdin
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
if b, err = cmd.Output(); err != nil {
|
if t.Set != "" {
|
||||||
return "", err
|
bytes, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
os.Setenv(t.Set, string(bytes))
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return string(b), nil
|
cmd.Stdout = os.Stdout
|
||||||
|
if err = cmd.Run(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readTaskfile() (tasks map[string]*Task, err error) {
|
func readTaskfile() (tasks map[string]*Task, err error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user