forked from melbahja/goph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
226 lines (186 loc) · 4.41 KB
/
cmd.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright 2020 Mohammed El Bahja. All rights reserved.
// Use of this source code is governed by a MIT license.
package goph
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
)
type remoteScriptType byte
const (
cmdLine remoteScriptType = iota
rawScript
scriptFile
)
// Cmd it's like os/exec.Cmd but for ssh session.
type Cmd struct {
// Path to command executable filename
Path string
// Command args.
Args []string
// Session env vars.
Env []string
// SSH session.
*ssh.Session
// Context for cancellation
Context context.Context
_type remoteScriptType
script *bytes.Buffer
// scriptFile string
stdout io.Writer
stderr io.Writer
}
func appendStringToBuffer(str string, buf bytes.Buffer) *bytes.Buffer {
parentStr := buf.String()
parentStr = fmt.Sprintf("%v \n %v", str, parentStr)
return bytes.NewBufferString(parentStr)
}
// CombinedOutput runs cmd on the remote host and returns its combined stdout and stderr.
func (c *Cmd) CombinedOutput() ([]byte, error) {
if err := c.init(); err != nil {
return nil, errors.Wrap(err, "cmd init")
}
return c.runWithContext(func() ([]byte, error) {
return c.Session.CombinedOutput(c.String())
})
}
// Output runs cmd on the remote host and returns its stdout.
func (c *Cmd) Output() ([]byte, error) {
if err := c.init(); err != nil {
return nil, errors.Wrap(err, "cmd init")
}
return c.runWithContext(func() ([]byte, error) {
return c.Session.Output(c.String())
})
}
// Output runs cmd on the remote host and returns its stdout.
func (c *Cmd) ScriptOutput() ([]byte, error) {
if err := c.init(); err != nil {
return nil, errors.Wrap(err, "cmd init")
}
var (
stdout bytes.Buffer
stderr bytes.Buffer
)
c.stdout = &stdout
c.stderr = &stderr
return c.runWithContext(func() ([]byte, error) {
err := c.run()
if err != nil {
return stderr.Bytes(), err
}
return stdout.Bytes(), err
})
}
func (c *Cmd) run() error {
if c._type == cmdLine {
return c.runCmds()
} else if c._type == rawScript {
return c.runScript()
} else if c._type == scriptFile {
return nil
// return c.runScriptFile()
} else {
return errors.New("Not supported RemoteScript type")
}
}
func (c *Cmd) runCmd(cmd string) error {
c.Session.Stdout = c.stdout
c.Session.Stderr = c.stderr
if err := c.Session.Run(cmd); err != nil {
return err
}
return nil
}
func (c *Cmd) runCmds() error {
for {
statment, err := c.script.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
return err
}
if err := c.runCmd(statment); err != nil {
return err
}
}
return nil
}
func (c *Cmd) runScript() error {
envs := strings.Join(c.Env, "\n")
c.script = appendStringToBuffer(fmt.Sprintf("%v \n", envs), *c.script)
c.Session.Stdin = c.script
c.Session.Stdout = c.stdout
c.Session.Stderr = c.stderr
if err := c.Session.Shell(); err != nil {
return err
}
if err := c.Session.Wait(); err != nil {
return err
}
return nil
}
// Run runs cmd on the remote host.
func (c *Cmd) Run() error {
if err := c.init(); err != nil {
return errors.Wrap(err, "cmd init")
}
_, err := c.runWithContext(func() ([]byte, error) {
return nil, c.Session.Run(c.String())
})
return err
}
// Start runs the command on the remote host.
func (c *Cmd) Start() error {
if err := c.init(); err != nil {
return errors.Wrap(err, "cmd init")
}
return c.Session.Start(c.String())
}
// String return the command line string.
func (c *Cmd) String() string {
return fmt.Sprintf("%s %s", c.Path, strings.Join(c.Args, " "))
}
// Init inits and sets session env vars.
func (c *Cmd) init() (err error) {
if c._type == rawScript {
return
}
// Set session env vars
var env []string
for _, value := range c.Env {
env = strings.Split(value, "=")
if err = c.Setenv(env[0], strings.Join(env[1:], "=")); err != nil {
return
}
}
return nil
}
// Command with context output.
type ctxCmdOutput struct {
output []byte
err error
}
// Executes the given callback within session. Sends SIGINT when the context is canceled.
func (c *Cmd) runWithContext(callback func() ([]byte, error)) ([]byte, error) {
outputChan := make(chan ctxCmdOutput)
go func() {
output, err := callback()
outputChan <- ctxCmdOutput{
output: output,
err: err,
}
}()
select {
case <-c.Context.Done():
_ = c.Session.Signal(ssh.SIGINT)
return nil, c.Context.Err()
case result := <-outputChan:
return result.output, result.err
}
}