forked from DarthSim/hivemind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
90 lines (71 loc) · 1.53 KB
/
process.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
package main
import (
"fmt"
"os"
"os/exec"
"syscall"
)
type process struct {
*exec.Cmd
Name string
Color int
CanDie bool
output *multiOutput
}
func newProcess(name, command string, color int, root string, port int, canDie bool, shell string, output *multiOutput) (proc *process) {
proc = &process{
exec.Command(fmt.Sprintf("/bin/%s", shell), "-c", command),
name,
color,
canDie,
output,
}
proc.Dir = root
proc.Env = append(os.Environ(), fmt.Sprintf("PORT=%d", port))
proc.output.Connect(proc)
return
}
func (p *process) writeLine(b []byte) {
p.output.WriteLine(p, b)
}
func (p *process) writeErr(err error) {
p.output.WriteErr(p, err)
}
func (p *process) signal(sig os.Signal) {
group, err := os.FindProcess(-p.Process.Pid)
if err != nil {
p.writeErr(err)
return
}
if err = group.Signal(sig); err != nil {
p.writeErr(err)
}
}
func (p *process) Running() bool {
return p.Process != nil && p.ProcessState == nil
}
func (p *process) Run() int {
p.output.PipeOutput(p)
defer p.output.ClosePipe(p)
ensureKill(p)
p.writeLine([]byte("\033[1mRunning...\033[0m"))
if err := p.Cmd.Run(); err != nil {
p.writeErr(err)
return err.(*exec.ExitError).ExitCode()
} else {
p.writeLine([]byte("\033[1mProcess exited\033[0m"))
return 0
}
}
func (p *process) Interrupt() {
if p.Running() {
p.writeLine([]byte("\033[1mInterrupting...\033[0m"))
p.signal(syscall.SIGINT)
}
}
func (p *process) Kill() {
if p.Running() {
p.writeLine([]byte("\033[1mKilling...\033[0m"))
p.signal(syscall.SIGKILL)
}
}