-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
127 lines (107 loc) · 2.74 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
var (
source = flag.String("source", "vendor", "source directory")
target = flag.String("target", "", "target directory (defaults to $GOBIN, if not set $GOPATH/bin)")
commands = flag.String("commands", "", "comma separated list of commands to execute after go install in temporary environment")
quiet = flag.Bool("quiet", false, "disable output")
)
func main() {
flag.Parse()
packages := flag.Args()
if len(packages) < 1 {
fail(errors.New("no packages: specify a package"))
}
gopath, err := ioutil.TempDir("", "go-vendorinstall-gopath")
if err != nil {
fail(err)
}
print(fmt.Sprintf("gopath: %s", gopath))
defer func() {
if err := os.RemoveAll(gopath); err != nil {
fail(err)
}
}()
if len(*target) == 0 {
if gobin := os.Getenv("GOBIN"); len(gobin) > 0 {
target = &gobin
} else {
bin := fmt.Sprintf("%s/bin", os.Getenv("GOPATH"))
target = &bin
}
}
gobin, err := filepath.Abs(*target)
if err != nil {
fail(err)
}
print(fmt.Sprintf("gobin: %s", gobin))
if err := link(gopath, *source); err != nil {
fail(err)
}
oldpath := os.Getenv("PATH")
path := fmt.Sprintf("%s%s%s", gobin, string(os.PathListSeparator), os.Getenv("PATH"))
os.Setenv("PATH", fmt.Sprintf("%s%s%s", gobin, string(os.PathListSeparator), os.Getenv("PATH")))
defer os.Setenv("PATH", oldpath)
env := []string{fmt.Sprintf("PATH=%s", path), fmt.Sprintf("GOPATH=%s", gopath), fmt.Sprintf("GOBIN=%s", gobin)}
args := append([]string{"install"}, packages...)
if out, err := doexec("go", gopath, args, env); err != nil {
print(string(out))
fail(err)
}
if len(*commands) > 0 {
for _, cmd := range strings.Split(*commands, ",") {
split := strings.Split(cmd, " ")
if out, err := doexec(split[0], gopath, split[1:], env); err != nil {
print(string(out))
fail(err)
}
}
}
}
func print(msg string) {
if !*quiet {
fmt.Println(msg)
}
}
func fail(err error) {
fmt.Printf("error: %s", err.Error())
os.Exit(1)
}
func link(gopath, source string) error {
srcdir, err := filepath.Abs(source)
if err != nil {
return err
}
linkto := filepath.Join(gopath, "src")
if err := os.MkdirAll(linkto, 0777); err != nil {
return err
}
files, err := ioutil.ReadDir(srcdir)
if err != nil {
return err
}
for _, file := range files {
real := filepath.Join(srcdir, file.Name())
link := filepath.Join(linkto, file.Name())
if err := os.Symlink(real, link); err != nil {
return err
}
}
return nil
}
func doexec(bin, dir string, args []string, env []string) ([]byte, error) {
print(fmt.Sprintf("%s %s", bin, strings.Join(args, " ")))
cmd := exec.Command(bin, args...)
cmd.Env = env
cmd.Dir = dir
return cmd.CombinedOutput()
}