-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreparations.go
80 lines (66 loc) · 1.65 KB
/
preparations.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 main
import (
"errors"
"go.uber.org/zap"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"syscall"
)
func checkArgs() error {
logger.Debug("[checkArgs]", zap.Strings("args", os.Args))
if len(os.Args) != 3 {
return errors.New("len(args) != 3")
}
debug := os.Args[1]
if debug != "debug" {
return errors.New("only support `debug`")
}
if path.Ext(os.Args[2]) != ".go" {
return errors.New("please input .go file")
}
return nil
}
func absoluteFilename() (string, error) {
filename := os.Args[2]
if path.IsAbs(filename) {
return filename, nil
}
dir, err := os.Getwd()
if err != nil {
return "", err
}
filename = path.Join(dir, filename)
_, err = os.Stat(filename)
if err != nil {
return "", err
}
return filename, nil
}
func build (filename string) (string, error) {
base := filepath.Base(filename)
execfile := path.Join(os.TempDir(), "__" + base+"__")
args := []string{"build", "-gcflags", "all=-N -l", "-o", execfile, filename}
cmd := exec.Command("go", args...)
return execfile, cmd.Run()
}
// not supoort arguments of cmds
func runexec(execfile string) (*exec.Cmd, error){
cmd := exec.Command(execfile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{Ptrace: true, Setpgid: true, Foreground: false}
// !!! maybe the diffrences of routine and thread in golang
runtime.LockOSThread()
if err := cmd.Start(); err != nil {
logger.Error("runexec:cmd.Start()", zap.Error(err))
return nil, err
}
if err := cmd.Wait(); err != nil && err.Error() != "stop signal: trace/breakpoint trap" {
logger.Error("runexec:cmd.Wait()", zap.Error(err))
return nil, err
}
return cmd, nil
}