forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
62 lines (48 loc) · 1.96 KB
/
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
package powershell
import (
"fmt"
"strings"
"github.com/cloudfoundry/bosh-utils/system"
)
const Executable = "powershell.exe"
type Runner struct {
BaseCmdRunner system.CmdRunner
}
func (c *Runner) RunComplexCommand(cmd system.Command) (stdout, stderr string, exitStatus int, err error) {
return c.BaseCmdRunner.RunComplexCommand(powershellCommand(cmd))
}
func (c *Runner) RunComplexCommandAsync(cmd system.Command) (system.Process, error) {
return c.BaseCmdRunner.RunComplexCommandAsync(powershellCommand(cmd))
}
func (c *Runner) RunCommand(cmdName string, args ...string) (stdout, stderr string, exitStatus int, err error) {
powershellArgs := append([]string{cmdName}, args...)
stdout, stderr, exitStatus, err = c.BaseCmdRunner.RunCommand(Executable, powershellArgs...)
if err != nil {
commandRan := strings.Join(append([]string{Executable}, powershellArgs...), " ")
if exitStatus == -1 {
wrappedErr := fmt.Errorf("Failed to run command \"%s\": %s", commandRan, err.Error())
return stdout, stderr, exitStatus, wrappedErr
}
if exitStatus > 0 {
wrappedErr := fmt.Errorf("Command \"%s\" exited with failure: %s", commandRan, stderr)
return stdout, stderr, exitStatus, wrappedErr
}
}
return
}
func (c *Runner) RunCommandQuietly(cmdName string, args ...string) (stdout, stderr string, exitStatus int, err error) {
return c.BaseCmdRunner.RunCommandQuietly(Executable, append([]string{cmdName}, args...)...)
}
func (c *Runner) RunCommandWithInput(input, cmdName string, args ...string) (stdout, stderr string, exitStatus int, err error) {
return c.BaseCmdRunner.RunCommandWithInput(input, Executable, append([]string{cmdName}, args...)...)
}
func (c *Runner) CommandExists(cmdName string) (exists bool) {
_, _, exitStatus, _ := c.RunCommand("Get-Command", cmdName)
return exitStatus == 0
}
func powershellCommand(cmd system.Command) system.Command {
newCmdArgs := append([]string{cmd.Name}, cmd.Args...)
cmd.Name = Executable
cmd.Args = newCmdArgs
return cmd
}