File: command.go

package info (click to toggle)
golang-github-masterzen-winrm 0.0~git20160323.0752679-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 256 kB
  • ctags: 180
  • sloc: makefile: 34; sh: 14
file content (210 lines) | stat: -rw-r--r-- 4,846 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package winrm

import (
	"bytes"
	"errors"
	"io"
	"strings"
)

type commandWriter struct {
	*Command
	eof bool
}

type commandReader struct {
	*Command
	write  *io.PipeWriter
	read   *io.PipeReader
	stream string
}

// Command represents a given command running on a Shell. This structure allows to get access
// to the various stdout, stderr and stdin pipes.
type Command struct {
	client    *Client
	shell     *Shell
	commandId string
	exitCode  int
	finished  bool
	err       error

	Stdin  *commandWriter
	Stdout *commandReader
	Stderr *commandReader

	done   chan struct{}
	cancel chan struct{}
}

func newCommand(shell *Shell, commandId string) *Command {
	command := &Command{shell: shell, client: shell.client, commandId: commandId, exitCode: 1, err: nil, done: make(chan struct{}), cancel: make(chan struct{})}
	command.Stdin = &commandWriter{Command: command, eof: false}
	command.Stdout = newCommandReader("stdout", command)
	command.Stderr = newCommandReader("stderr", command)

	go fetchOutput(command)

	return command
}

func newCommandReader(stream string, command *Command) *commandReader {
	read, write := io.Pipe()
	return &commandReader{Command: command, stream: stream, write: write, read: read}
}

func fetchOutput(command *Command) {
	for {
		select {
		case <-command.cancel:
			close(command.done)
			return
		default:
			finished, err := command.slurpAllOutput()
			if finished {
				command.err = err
				close(command.done)
				return
			}
		}
	}
}

func (command *Command) check() (err error) {
	if command.commandId == "" {
		return errors.New("Command has already been closed")
	}
	if command.shell == nil {
		return errors.New("Command has no associated shell")
	}
	if command.client == nil {
		return errors.New("Command has no associated client")
	}
	return
}

// Close will terminate the running command
func (command *Command) Close() (err error) {
	if err = command.check(); err != nil {
		return err
	}

	select { // close cancel channel if it's still open
	case <-command.cancel:
	default:
		close(command.cancel)
	}

	request := NewSignalRequest(command.client.url, command.shell.ShellId, command.commandId, &command.client.Parameters)
	defer request.Free()

	_, err = command.client.sendRequest(request)
	return err
}

func (command *Command) slurpAllOutput() (finished bool, err error) {
	if err = command.check(); err != nil {
		command.Stderr.write.CloseWithError(err)
		command.Stdout.write.CloseWithError(err)
		return true, err
	}

	request := NewGetOutputRequest(command.client.url, command.shell.ShellId, command.commandId, "stdout stderr", &command.client.Parameters)
	defer request.Free()

	response, err := command.client.sendRequest(request)
	if err != nil {
		if strings.Contains(err.Error(), "OperationTimeout") {
			// Operation timeout because there was no command output
			return
		}

		command.Stderr.write.CloseWithError(err)
		command.Stdout.write.CloseWithError(err)
		return true, err
	}

	var exitCode int
	var stdout, stderr bytes.Buffer
	finished, exitCode, err = ParseSlurpOutputErrResponse(response, &stdout, &stderr)
	if err != nil {
		command.Stderr.write.CloseWithError(err)
		command.Stdout.write.CloseWithError(err)
		return true, err
	}
	if stdout.Len() > 0 {
		command.Stdout.write.Write(stdout.Bytes())
	}
	if stderr.Len() > 0 {
		command.Stderr.write.Write(stderr.Bytes())
	}
	if finished {
		command.exitCode = exitCode
		command.Stderr.write.Close()
		command.Stdout.write.Close()
	}

	return
}

func (command *Command) sendInput(data []byte) (err error) {
	if err = command.check(); err != nil {
		return err
	}

	request := NewSendInputRequest(command.client.url, command.shell.ShellId, command.commandId, data, &command.client.Parameters)
	defer request.Free()

	_, err = command.client.sendRequest(request)
	return
}

// ExitCode returns command exit code when it is finished. Before that the result is always 0.
func (command *Command) ExitCode() int {
	return command.exitCode
}

// Calling this function will block the current goroutine until the remote command terminates.
func (command *Command) Wait() {
	// block until finished
	<-command.done
}

// Write data to this Pipe
func (w *commandWriter) Write(data []byte) (written int, err error) {
	for len(data) > 0 {
		if w.eof {
			err = io.EOF
			return
		}
		// never send more data than our EnvelopeSize.
		n := min(w.client.Parameters.EnvelopeSize-1000, len(data))
		if err = w.sendInput(data[:n]); err != nil {
			break
		}
		data = data[n:]
		written += int(n)
	}
	return
}

func min(a int, b int) int {
	if a < b {
		return a
	}
	return b
}

func (w *commandWriter) Close() error {
	w.eof = true
	return w.Close()
}

// Read data from this Pipe
func (r *commandReader) Read(buf []byte) (int, error) {
	n, err := r.read.Read(buf)
	if err != nil && err != io.EOF {
		return 0, err
	}
	return n, err
}