/
shell.go
185 lines (170 loc) · 3.78 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
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"sync"
"time"
)
func pip(args []string) {
config := readConfig()
localPip := config.Local.Pip
// _ = _execCommand(localPip, args)
proc := NewProcess(localPip, args...)
out := proc.StreamOutput()
file, err := os.OpenFile("system.log", os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
go func() {
for out.Scan() {
_, err := file.WriteString(out.Text() + "\n")
if err != nil {
log.Fatalf("failed writing to file: %s", err)
}
defer file.Close()
log.Println(out.Text())
}
}()
proc.Start()
}
func python(args []string) {
config := readConfig()
localPython := config.Local.Python
_ = _execCommand(localPython, args)
}
func _execCommand(commandName string, params []string) bool {
cmd := exec.Command(commandName, params...)
cmd.Stdout = os.Stdout
fmt.Println(cmd.Args)
return true
}
type Process struct {
proc *exec.Cmd
cancellationSignal chan uint8
done chan error
returnCode chan error
started bool
stdOutRead *io.PipeReader
stdOutWrite *io.PipeWriter
inputWriter *io.PipeWriter
inputStreamSet bool
outputStreamSet bool
completed bool
timeout time.Duration
// Access to completed MUST capture the lock.
mutex sync.RWMutex
}
func NewProcess(command string, args ...string) *Process {
process := &Process{
exec.Command(command, args...),
make(chan uint8, 1),
make(chan error, 1),
make(chan error, 1),
false,
&io.PipeReader{},
&io.PipeWriter{},
&io.PipeWriter{},
false,
false,
false,
0,
sync.RWMutex{}}
return process
}
func (p *Process) Start() *Process {
if p.timeout > 0 {
log.Println("Its greater than 0")
timer := time.NewTimer(p.timeout)
go func() {
<-timer.C
p.Kill()
}()
}
p.started = true
//Call the other functions to stream stdin and stdout
err := p.proc.Start()
if err != nil {
panic(err)
}
go p.awaitOutput()
go p.finishTimeOutOrDie()
return p
}
func (p *Process) SetTimeout(d time.Duration) {
if p.started {
panic("Can not set timeout after process started")
}
p.timeout = d
}
func (p *Process) Wait() error {
return <-p.returnCode
}
func (p *Process) awaitOutput() {
//send the exit code to the done channel to signify the command finished
p.done <- p.proc.Wait()
}
func (p *Process) Kill() {
p.mutex.Lock()
if !p.completed {
p.cancellationSignal <- 1
}
p.mutex.Unlock()
}
func (p *Process) OpenInputStream() (io.WriteCloser, error) {
if p.inputStreamSet {
panic("Input stream already set")
}
if p.started {
panic("process already started")
}
stdIn, err := p.proc.StdinPipe()
p.inputStreamSet = true
return stdIn, err
}
func (p *Process) StreamOutput() *bufio.Scanner {
//pipe both stdout and stderr into the same pipe
//panics if you do streamoutput after proccess starting or
//if the output stream is already set
if p.started {
panic("Cant set output stream after starting")
}
if p.outputStreamSet {
panic("output stream already set!")
}
p.stdOutRead, p.stdOutWrite = io.Pipe()
p.proc.Stdout = p.stdOutWrite
p.proc.Stderr = p.stdOutWrite
p.outputStreamSet = true
//return a scanner which they can read from till empty
return bufio.NewScanner(p.stdOutRead)
}
func (p *Process) finishTimeOutOrDie() {
defer p.cleanup()
var result error
select {
case result = <-p.done:
case <-p.cancellationSignal:
log.Println("received cancellationSignal")
//NOT PORTABLE TO WINDOWS
err := p.proc.Process.Kill()
if err != nil {
log.Println(err)
}
}
p.returnCode <- result
}
func (p *Process) cleanup() {
p.mutex.Lock()
p.completed = true
p.mutex.Unlock()
if p.outputStreamSet {
p.stdOutRead.Close()
p.stdOutWrite.Close()
}
close(p.done)
close(p.cancellationSignal)
}