forked from cloudfoundry/bosh-bootloader
/
command.go
49 lines (38 loc) · 793 Bytes
/
command.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
package bosh
import (
"fmt"
"io"
"os/exec"
)
type Cmd struct {
stderr io.Writer
}
func NewCmd(stderr io.Writer) Cmd {
return Cmd{
stderr: stderr,
}
}
func (c Cmd) GetBOSHPath() (string, error) {
var boshPath = "bosh"
path, err := exec.LookPath("bosh2")
if err != nil {
if err.(*exec.Error).Err != exec.ErrNotFound {
return "", fmt.Errorf("failed when searching for BOSH: %s", err) // not tested
}
}
if path != "" {
boshPath = path
}
return boshPath, nil
}
func (c Cmd) Run(stdout io.Writer, workingDirectory string, args []string) error {
boshPath, err := c.GetBOSHPath()
if err != nil {
return err
}
command := exec.Command(boshPath, args...)
command.Dir = workingDirectory
command.Stdout = stdout
command.Stderr = c.stderr
return command.Run()
}