forked from gravityblast/fresh
/
runner.go
79 lines (65 loc) · 1.43 KB
/
runner.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
package runner
import (
"context"
"io"
"os/exec"
"strconv"
)
func getDebugCommand(pid int) (string, []string) {
return "dlv", []string{"attach",
strconv.Itoa(pid),
"--listen=:40000",
"--headless",
"--api-version=2",
"--accept-multiclient"}
// "--log",
}
// KillFn specifies the type for function which kills processes spawned by fresh-dlv
type KillFn func()
func run(debug bool) KillFn {
runnerLog("Running (isDebug: %v)...", debug)
ctx, cancel := context.WithCancel(context.Background())
var debugCmd *exec.Cmd
var cancelDebugger context.CancelFunc
cmd := exec.CommandContext(ctx, buildPath())
stderr, err := cmd.StderrPipe()
if err != nil {
fatal(err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
fatal(err)
}
err = cmd.Start()
if err != nil {
fatal(err)
}
writer := appLogWriter{}
if debug {
writer := debuggerLogWriter{}
cname, args := getDebugCommand(cmd.Process.Pid)
ctx, cancelDebugger = context.WithCancel(context.Background())
debugCmd = exec.CommandContext(ctx, cname, args...)
stdout, err := debugCmd.StdoutPipe()
if err != nil {
fatal(err)
}
stderr, err := debugCmd.StderrPipe()
if err != nil {
fatal(err)
}
if err := debugCmd.Start(); err != nil {
fatal(err)
}
go io.Copy(writer, stderr)
go io.Copy(writer, stdout)
}
go io.Copy(writer, stderr)
go io.Copy(writer, stdout)
return func() {
cancel()
if cancelDebugger != nil {
cancelDebugger()
}
}
}