-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.go
191 lines (168 loc) · 3.53 KB
/
shell.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
package ppow
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"sync"
"github.com/dottedmag/termlog"
)
var ValidShells = map[string]bool{
"bash": true,
"powershell": true,
"sh": true,
}
var shellTesting bool
var Default = "sh"
type Executor struct {
Shell string
Command string
Dir string
cmd *exec.Cmd
stdo io.ReadCloser
stde io.ReadCloser
sync.Mutex
}
type ExecState struct {
Error error
ErrOutput string
ProcState string
}
func GetShellName(v string) (string, error) {
if v == "" {
return Default, nil
}
if _, ok := ValidShells[v]; !ok {
return "", fmt.Errorf("Unsupported shell: %q", v)
}
return v, nil
}
func NewExecutor(shell string, command string, dir string) (*Executor, error) {
_, err := makeCommand(shell, command, dir)
if err != nil {
return nil, err
}
return &Executor{
Shell: shell,
Command: command,
Dir: dir,
}, nil
}
func (e *Executor) start(
log termlog.Stream, bufferr bool,
) (*exec.Cmd, *bytes.Buffer, *sync.WaitGroup, error) {
e.Lock()
defer e.Unlock()
cmd, err := makeCommand(e.Shell, e.Command, e.Dir)
if err != nil {
return nil, nil, nil, err
}
e.cmd = cmd
stdo, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, nil, err
}
stde, err := cmd.StderrPipe()
if err != nil {
return nil, nil, nil, err
}
e.stdo = stdo
e.stde = stde
buff := new(bytes.Buffer)
err = cmd.Start()
if err != nil {
return nil, nil, nil, err
}
wg := sync.WaitGroup{}
wg.Add(2)
buflock := sync.Mutex{}
go logOutput(
&wg, stde,
func(s string, args ...interface{}) {
log.Warn(s, args...)
if bufferr {
buflock.Lock()
defer buflock.Unlock()
fmt.Fprintf(buff, "%s\n", args...)
}
},
)
go logOutput(&wg, stdo, log.Say)
return cmd, buff, &wg, nil
}
func (e *Executor) running() bool {
return e.cmd != nil
}
func (e *Executor) Running() bool {
e.Lock()
defer e.Unlock()
return e.running()
}
func (e *Executor) reset() {
e.Lock()
defer e.Unlock()
e.cmd = nil
}
func (e *Executor) Run(log termlog.Stream, bufferr bool) (error, *ExecState) {
if e.cmd != nil {
return fmt.Errorf("already running"), nil
}
cmd, buff, wg, err := e.start(log, bufferr)
if err != nil {
return err, nil
}
// Order is important here. We MUST wait for the readers to exit before we wait
// on the command itself.
wg.Wait()
eret := cmd.Wait()
estate := &ExecState{
Error: eret,
ErrOutput: buff.String(),
ProcState: cmd.ProcessState.String(),
}
e.reset()
return nil, estate
}
func (e *Executor) Signal(sig os.Signal) error {
e.Lock()
defer e.Unlock()
if !e.running() {
return fmt.Errorf("executor not running")
}
return e.sendSignal(sig)
}
// CheckShell checks that a shell is supported, and returns the correct command name
func CheckShell(shell string) (string, error) {
if _, ok := ValidShells[shell]; !ok {
return "", fmt.Errorf("unsupported shell: %q", shell)
}
switch shell {
case "powershell":
if _, err := exec.LookPath("powershell"); err == nil {
return "powershell", nil
} else if _, err := exec.LookPath("pwsh"); err == nil {
return "pwsh", nil
} else {
return "", fmt.Errorf("powershell/pwsh not on path")
}
default:
return exec.LookPath(shell)
}
}
func makeCommand(shell string, command string, dir string) (*exec.Cmd, error) {
shcmd, err := CheckShell(shell)
if err != nil {
return nil, err
}
var cmd *exec.Cmd
switch shell {
case "bash", "sh":
cmd = exec.Command(shcmd, "-c", command)
case "powershell":
cmd = exec.Command(shcmd, "-Command", command)
}
cmd.Dir = dir
prepCmd(cmd)
return cmd, nil
}