This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
tool.go
160 lines (138 loc) · 3.2 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
type tool struct {
Repository string // eg "github.com/tools/godep"
Commit string // eg "3020345802e4bff23902cfc1d19e90a79fae714e"
ref string // eg "origin/master"
Fork string `json:"Fork,omitempty"` // eg "code.jusin.tv/twitch/godep"
}
func (t *tool) path() string {
return filepath.Join(toolDirPath, "src", t.Repository)
}
func (t *tool) executable() string {
return filepath.Base(t.Repository)
}
func setEnvVar(cmd *exec.Cmd, key, val string) {
var env []string
if cmd.Env != nil {
env = cmd.Env
} else {
env = os.Environ()
}
envSet := false
for i, envVar := range env {
if strings.HasPrefix(envVar, key+"=") {
env[i] = key + "=" + val
envSet = true
}
}
if !envSet {
env = append(env, key+"="+val)
}
cmd.Env = env
}
func get(t *tool) error {
log("downloading " + t.Repository)
cmd := exec.Command("go", "get", "-d", t.Repository)
setEnvVar(cmd, "GOPATH", toolDirPath)
_, err := cmd.Output()
if err != nil {
return errors.Wrap(err, "failed to 'go get' tool")
}
return err
}
func setVersion(t *tool) error {
// If we're using a fork, add it
if t.Fork != "" {
cmd := exec.Command("git", "remote")
cmd.Dir = t.path()
b, err := cmd.Output()
if err != nil {
return err
}
rs := strings.Split(string(b), "\n")
containsFork := false
for _, r := range rs {
if r == "fork" {
containsFork = true
break
}
}
if containsFork {
cmd = exec.Command("git", "remote", "rm", "fork")
cmd.Dir = t.path()
_, err = cmd.Output()
if err != nil {
return err
}
}
cmd = exec.Command("git", "remote", "add", "-f", "fork", t.Fork)
cmd.Dir = t.path()
_, err = cmd.Output()
if err != nil {
return err
}
}
log("setting version for " + t.Repository)
cmd := exec.Command("git", "fetch")
cmd.Dir = t.path()
_, err := cmd.Output()
if err != nil {
return err
}
// If we have a symbolic reference, parse it
if t.ref != "" {
log(fmt.Sprintf("parsing revision %q", t.ref))
cmd = exec.Command("git", "rev-parse", t.ref)
cmd.Dir = t.path()
var out []byte
out, err = cmd.Output()
if err != nil {
return err
}
t.Commit = strings.TrimSpace(string(out))
log(fmt.Sprintf("parsed as %q", t.Commit))
}
cmd = exec.Command("git", "checkout", t.Commit)
cmd.Dir = t.path()
_, err = cmd.Output()
if err != nil {
return errors.Wrap(err, "failed to 'git checkout' tool")
}
// Re-run 'go get' in case the new version has a different set of dependencies.
cmd = exec.Command("go", "get", "-d", t.Repository)
setEnvVar(cmd, "GOPATH", toolDirPath)
_, err = cmd.Output()
if err != nil {
return errors.Wrap(err, "failed to 'go get' tool")
}
return err
}
func download(t *tool) error {
err := get(t)
if err != nil {
fatalExec("go get -d "+t.Repository, err)
}
err = setVersion(t)
if err != nil {
fatalExec("git checkout "+t.Commit, err)
}
return nil
}
func install(t *tool) error {
log("installing " + t.Repository)
cmd := exec.Command("go", "install", t.Repository)
setEnvVar(cmd, "GOPATH", toolDirPath)
_, err := cmd.Output()
if err != nil {
return errors.Wrap(err, "failed to 'go install' tool")
}
return err
}