forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winrm_command_runner.go
67 lines (47 loc) · 1.86 KB
/
winrm_command_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
65
66
67
package utils
import (
"errors"
"github.com/masterzen/winrm"
"bytes"
"fmt"
"strings"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type WinRMCommandRunner struct {
Client *winrm.Client
}
func (r *WinRMCommandRunner) RunComplexCommand(cmd boshsys.Command) (stdout, stderr string, exitStatus int, err error) {
outBytes := &bytes.Buffer{}
errBytes := &bytes.Buffer{}
var exitCode int
cmdName := cmd.Name
cmdArgs := cmd.Args
if cmdName == "powershell.exe" {
cmdName = cmdArgs[0]
cmdArgs = cmdArgs[1:]
}
powerShellCommand := winrm.Powershell(fmt.Sprintf("%s %s", cmdName, strings.Join(cmdArgs, " ")))
if cmd.Stdin != nil {
exitCode, err = r.Client.RunWithInput(powerShellCommand, outBytes, errBytes, cmd.Stdin)
} else {
exitCode, err = r.Client.Run(winrm.Powershell(powerShellCommand), outBytes, errBytes)
}
outString := outBytes.String()
errString := errBytes.String()
return outString, errString, exitCode, err
}
func (r *WinRMCommandRunner) RunComplexCommandAsync(cmd boshsys.Command) (boshsys.Process, error) {
return nil, errors.New("Asynchronous commands not supported with this winRM implementation")
}
func (r *WinRMCommandRunner) RunCommand(cmdName string, args ...string) (stdout, stderr string, exitStatus int, err error) {
return r.RunComplexCommand(boshsys.Command{Name: cmdName, Args: args})
}
func (r *WinRMCommandRunner) RunCommandQuietly(cmdName string, args ...string) (stdout, stderr string, exitStatus int, err error) {
return r.RunComplexCommand(boshsys.Command{Name: cmdName, Args: args})
}
func (r *WinRMCommandRunner) RunCommandWithInput(input, cmdName string, args ...string) (stdout, stderr string, exitStatus int, err error) {
return r.RunComplexCommand(boshsys.Command{Name: cmdName, Args: args, Stdin: strings.NewReader(input)})
}
func (r *WinRMCommandRunner) CommandExists(cmdName string) (exists bool) {
return false
}