-
Notifications
You must be signed in to change notification settings - Fork 8
/
tool.go
107 lines (97 loc) · 1.93 KB
/
tool.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
package main
import (
"fmt"
"go/build"
"log"
"os"
"os/exec"
//"runtime"
"path"
"strings"
)
func init() {
log.SetPrefix("goblin:\t ")
log.SetFlags(0)
}
func getPkgPath() (string, error) {
p, err := exec.LookPath(os.Args[0])
if err != nil {
return "", err
}
cmd := exec.Command("go", "tool", "objdump", "-s=main.main", p)
out, err := cmd.Output()
if err != nil {
return "", err
}
s := strings.SplitN(fmt.Sprintf("%s", out), "\n", 2)[0]
s = s[len("TEXT main.main(SB) "):]
//makesure this source file's name matches whats hardcoded here.
// Otherwise things break.
return s[:len(s)-len("/tool.go")], nil
}
func usage() (s string) {
install := "install:\tinstalls the goblin tools to $GOTOOLDIR"
return fmt.Sprintln(install)
}
func main() {
if len(os.Args) == 1 {
log.Println(usage())
return
}
for _, arg := range os.Args[1:] {
switch arg {
case "install":
install()
default:
log.Println("invalid input arg:", arg)
log.Println(usage())
return
}
}
}
func install() {
if p, err := getPkgPath(); err != nil {
log.Fatal(err)
} else {
if err := os.Chdir(p); err != nil {
log.Fatal(err)
}
}
if err := os.Chdir("cmd"); err != nil {
log.Fatal(err)
}
f, err := os.Open(".")
if err != nil {
log.Fatal(err)
}
dirs, err := f.Readdirnames(0)
if err != nil {
log.Fatal(err)
}
for _, d := range dirs {
if err := os.Chdir(d); err != nil {
log.Fatal(err)
}
if d, err := os.Getwd(); err != nil {
log.Fatal(err)
} else {
log.Println(d)
nm := path.Base(d)
out, err := exec.Command("go", "build").CombinedOutput()
if err != nil {
log.Printf("go build %s failed\n\n", nm)
fmt.Fprintf(os.Stderr, "[go-build-log]\n%s", out)
goto quit
}
log.Printf("mv %s to $GOTOOLDIR\n", nm)
if err := os.Rename(d+"/"+nm, build.ToolDir+"/"+nm); err != nil {
log.Fatal(err)
}
}
quit:
if err := os.Chdir(".."); err != nil {
log.Fatal(err)
}
fmt.Println()
}
}