This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
74 lines (68 loc) · 1.41 KB
/
main.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
package main
import (
"flag"
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
dry := flag.Bool("n", false, "don't build, just print the package names")
flag.Parse()
bins := flag.Args()
if len(bins) == 0 {
fi, err := ioutil.ReadDir(build.Default.GOPATH + "/bin")
if err != nil {
log.Fatal(err)
}
for _, f := range fi {
if f.IsDir() {
continue
}
bins = append(bins, build.Default.GOPATH+"/bin/"+f.Name())
}
}
var binDir string
if !*dry {
dir, err := ioutil.TempDir("", "gorebuild")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
binDir = dir
}
for _, file := range bins {
path, err := getMainPath(file)
if err != nil {
log.Printf("Skipping %s: %s", file, err)
continue
}
importPath := stripPath(path)
if *dry {
fmt.Println(stripPath(path))
} else {
cmd := exec.Command("go", "install", "-v", importPath)
cmd.Env = append(os.Environ(), "GOBIN="+binDir)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
cmd.Run()
fi, err := ioutil.ReadDir(binDir)
if err != nil {
log.Fatal(err)
}
for _, f := range fi {
err := os.Rename(filepath.Join(binDir, f.Name()), build.Default.GOPATH+"/bin/"+f.Name())
if err != nil {
log.Fatal(err)
}
}
}
}
}
func stripPath(path string) string {
dir := filepath.Dir(path)
return strings.TrimPrefix(dir, build.Default.GOPATH+"/src/")
}