1
0
mirror of https://github.com/go-task/task.git synced 2025-02-09 13:47:06 +02:00

execext package: support context command

This commit is contained in:
Andrey Nering 2017-04-12 20:32:56 -03:00
parent 947b3de0c4
commit 822f7f83ee
5 changed files with 16 additions and 11 deletions

View File

@ -1,6 +1,7 @@
package execext package execext
import ( import (
"context"
"os/exec" "os/exec"
) )
@ -17,6 +18,6 @@ func init() {
ShExists = err == nil ShExists = err == nil
} }
func newShCommand(c string) *exec.Cmd { func newShCommand(ctx context.Context, c string) *exec.Cmd {
return exec.Command(ShPath, "-c", c) return exec.CommandContext(ctx, ShPath, "-c", c)
} }

View File

@ -3,11 +3,12 @@
package execext package execext
import ( import (
"context"
"os/exec" "os/exec"
) )
// NewCommand returns a new command that runs on "sh" is available or on "cmd" // NewCommand returns a new command that runs on "sh" is available or on "cmd"
// otherwise on Windows // otherwise on Windows
func NewCommand(c string) *exec.Cmd { func NewCommand(ctx context.Context, c string) *exec.Cmd {
return newShCommand(c) return newShCommand(ctx, c)
} }

View File

@ -3,18 +3,19 @@
package execext package execext
import ( import (
"context"
"os/exec" "os/exec"
) )
// NewCommand returns a new command that runs on "sh" is available or on "cmd" // NewCommand returns a new command that runs on "sh" is available or on "cmd"
// otherwise on Windows // otherwise on Windows
func NewCommand(c string) *exec.Cmd { func NewCommand(ctx context.Context, c string) *exec.Cmd {
if ShExists { if ShExists {
return newShCommand(c) return newShCommand(ctx, c)
} }
return newCmdCommand(c) return newCmdCommand(ctx, c)
} }
func newCmdCommand(c string) *exec.Cmd { func newCmdCommand(ctx context.Context, c string) *exec.Cmd {
return exec.Command("cmd", "/C", c) return exec.CommandContext(ctx, "cmd", "/C", c)
} }

View File

@ -1,6 +1,7 @@
package task package task
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"os" "os"
@ -189,7 +190,7 @@ func (t *Task) runCommand(i int) error {
if err != nil { if err != nil {
return err return err
} }
cmd := execext.NewCommand(c) cmd := execext.NewCommand(context.Background(), c)
if dir != "" { if dir != "" {
cmd.Dir = dir cmd.Dir = dir
} }

View File

@ -2,6 +2,7 @@ package task
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io/ioutil"
@ -32,7 +33,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
} }
cmd := execext.NewCommand(value[1:]) cmd := execext.NewCommand(context.Background(), 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()