forked from wailsapp/wails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
75 lines (62 loc) · 1.29 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
package process
import (
"os"
"os/exec"
)
// Process defines a process that can be executed
type Process struct {
cmd *exec.Cmd
exitChannel chan bool
Running bool
}
// NewProcess creates a new process struct
func NewProcess(cmd string, args ...string) *Process {
result := &Process{
cmd: exec.Command(cmd, args...),
exitChannel: make(chan bool, 1),
}
result.cmd.Stdout = os.Stdout
result.cmd.Stderr = os.Stderr
return result
}
// Start the process
func (p *Process) Start(exitCodeChannel chan int) error {
err := p.cmd.Start()
if err != nil {
return err
}
p.Running = true
go func(cmd *exec.Cmd, running *bool, exitChannel chan bool, exitCodeChannel chan int) {
err := cmd.Wait()
if err == nil {
exitCodeChannel <- 0
}
*running = false
exitChannel <- true
}(p.cmd, &p.Running, p.exitChannel, exitCodeChannel)
return nil
}
// Kill the process
func (p *Process) Kill() error {
if !p.Running {
return nil
}
err := p.cmd.Process.Kill()
if err != nil {
return err
}
err = p.cmd.Process.Release()
if err != nil {
return err
}
// Wait for command to exit properly
<-p.exitChannel
return err
}
// PID returns the process PID
func (p *Process) PID() int {
return p.cmd.Process.Pid
}
func (p *Process) SetDir(dir string) {
p.cmd.Dir = dir
}