Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cwd and env support for shell #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import "net"
type Parameters struct {
Timeout string
Locale string
Cwd string
Env map[string]string
EnvelopeSize int
TransportDecorator func() Transporter
Dial func(network, addr string) (net.Conn, error)
Expand Down
12 changes: 12 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func NewOpenShellRequest(uri string, params *Parameters) *soap.SoapMessage {
input.SetContent("stdin")
output := message.CreateElement(body, "OutputStreams", soap.DOM_NS_WIN_SHELL)
output.SetContent("stdout stderr")
if len(params.Cwd) > 0 {
cwd := message.CreateElement(body, "WorkingDirectory", soap.DOM_NS_WIN_SHELL)
cwd.SetContent(params.Cwd)
}
if params.Env != nil {
env := message.CreateElement(body, "Environment", soap.DOM_NS_WIN_SHELL)
for name, value := range params.Env {
variable := message.CreateElement(env, "Variable", soap.DOM_NS_WIN_SHELL)
variable.SetAttr("Name", name)
variable.SetContent(value)
}
}

return message
}
Expand Down
9 changes: 8 additions & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ type WinRMSuite struct{}
var _ = Suite(&WinRMSuite{})

func (s *WinRMSuite) TestOpenShellRequest(c *C) {
openShell := NewOpenShellRequest("http://localhost", nil)
parameters := NewParameters("PT60S", "en-US", 153600)
parameters.Cwd = "c:\\"
parameters.Env = map[string]string{
"foo": "bar",
}
openShell := NewOpenShellRequest("http://localhost", parameters)
defer openShell.Free()

assertXPath(c, openShell.Doc(), "//a:Action", "http://schemas.xmlsoap.org/ws/2004/09/transfer/Create")
assertXPath(c, openShell.Doc(), "//a:To", "http://localhost")
assertXPath(c, openShell.Doc(), "//env:Body/rsp:Shell/rsp:InputStreams", "stdin")
assertXPath(c, openShell.Doc(), "//env:Body/rsp:Shell/rsp:OutputStreams", "stdout stderr")
assertXPath(c, openShell.Doc(), "//env:Body/rsp:Shell/rsp:WorkingDirectory", "c:\\")
assertXPath(c, openShell.Doc(), "//env:Body/rsp:Shell/rsp:Environment/rsp:Variable[@Name=\"foo\"]", "bar")
}

func (s *WinRMSuite) TestDeleteShellRequest(c *C) {
Expand Down