generated from golang-templates/seed
/
exec.go
29 lines (25 loc) · 900 Bytes
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package taskflow
import (
"os/exec"
"strings"
)
// Cmd is like exec.Command, but it assignes tf's context
// and assigns Stdout and Stderr to tf's output.
func (tf *TF) Cmd(name string, args ...string) *exec.Cmd {
cmdStr := strings.Join(append([]string{name}, args...), " ")
tf.Logf("Cmd: %s", cmdStr)
cmd := exec.CommandContext(tf.Context(), name, args...) //nolint:gosec // yes, this runs a subprocess
cmd.Stderr = tf.Output()
cmd.Stdout = tf.Output()
return cmd
}
// Exec returns a command that will run the named program with the given arguments.
// The command will pass only if the program if the program runs, has no problems
// copying stdin, stdout, and stderr, and exits with a zero exit status.
func Exec(name string, args ...string) func(*TF) {
return func(tf *TF) {
if err := tf.Cmd(name, args...).Run(); err != nil {
tf.Fatalf("Cmd %s failed: %v", name, err)
}
}
}