forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_runner.go
64 lines (52 loc) · 1.48 KB
/
cmd_runner.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package acceptance
import (
"fmt"
"io"
"strings"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type CmdRunner interface {
RunCommand(env map[string]string, args ...string) (string, string, int, error)
RunStreamingCommand(out io.Writer, env map[string]string, args ...string) (string, string, int, error)
}
type cmdRunner struct {
runner boshsys.CmdRunner
}
func NewCmdRunner(logger boshlog.Logger) CmdRunner {
return &cmdRunner{
runner: boshsys.NewExecCmdRunner(logger),
}
}
func (r *cmdRunner) RunCommand(env map[string]string, args ...string) (string, string, int, error) {
exports := make([]string, len(env))
for k, v := range env {
exports = append(exports, fmt.Sprintf("%s=%s", k, v))
}
argsWithEnv := append(exports, args...)
return r.runner.RunCommand(
"bash",
"-c",
strings.Join(argsWithEnv, " "),
)
}
func (r *cmdRunner) RunStreamingCommand(out io.Writer, env map[string]string, args ...string) (string, string, int, error) {
exports := make([]string, len(env))
for k, v := range env {
exports = append(exports, fmt.Sprintf("%s=%s", k, v))
}
argsWithEnv := append(exports, args...)
cmd := boshsys.Command{
Name: "bash",
Args: []string{
"-c",
strings.Join(argsWithEnv, " "),
},
Stdout: out,
Stderr: out,
}
// write command being run
cmdString := fmt.Sprintf("> %s %s\n", cmd.Name, strings.Join(cmd.Args, " "))
out.Write([]byte(cmdString))
return r.runner.RunComplexCommand(cmd)
}