-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
281 lines (253 loc) · 5.68 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2013-2014 Canonical Ltd.
// godeb dynamically translates stock upstream Go tarballs to deb packages.
//
// For details of how this tool works and context for why it was built,
// please refer to the following blog post:
//
// http://blog.labix.org/2013/06/15/in-flight-deb-packages-of-go
//
package main
import (
"bytes"
"fmt"
"go/build"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"sort"
"strings"
"gopkg.in/xmlpath.v1"
)
var usage = `Usage: gofetch <command> [<options> ...]
Available commands:
list
install [<version>]
download [<version>]
remove
`
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}
func run() error {
if len(os.Args) == 2 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
fmt.Println(usage)
return nil
}
if len(os.Args) < 2 {
fmt.Println(usage)
return fmt.Errorf("command missing")
}
if strings.HasPrefix(os.Args[1], "-") {
return fmt.Errorf("unknown option: %s", os.Args[1])
}
switch command := os.Args[1]; command {
case "list":
if len(os.Args) > 2 {
return fmt.Errorf("list command takes no arguments")
}
return listCommand()
case "download", "install":
version := ""
if len(os.Args) == 3 {
version = os.Args[2]
} else if len(os.Args) > 3 {
return fmt.Errorf("too many arguments to %s command", command)
}
return actionCommand(version, command == "install")
case "remove":
return removeCommand()
default:
return fmt.Errorf("unknown command: %s", os.Args[1])
}
}
func listCommand() error {
tbs, err := tarballs()
if err != nil {
return err
}
for _, tb := range tbs {
fmt.Println(tb.Version)
}
return nil
}
func removeCommand() error {
args := []string{"rm", "-rf", "/usr/local/go"}
if os.Getuid() != 0 {
args = append([]string{"sudo"}, args...)
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("while removing go: %v", err)
}
return nil
}
func actionCommand(version string, install bool) error {
tbs, err := tarballs()
if err != nil {
return err
}
var url string
if version == "" {
version = tbs[0].Version
url = tbs[0].URL
} else {
for _, tb := range tbs {
if version == tb.Version {
url = tb.URL
break
}
}
if url == "" {
var urls []string
for _, source := range tarballSources {
urls = append(urls, source.url)
}
return fmt.Errorf("version %s not available at %s", version, strings.Join(urls, " or "))
}
}
fmt.Println("processing", url)
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("failed to download %s: %v", url, err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("got status code %d", resp.StatusCode)
}
defer resp.Body.Close()
archiveName := fmt.Sprintf("go_%s.tar.gz", version)
archive, err := os.Create(archiveName)
if err != nil {
return fmt.Errorf("cannot create deb: %v", err)
}
defer archive.Close()
io.Copy(archive, resp.Body)
removeCommand()
args := []string{"tar", "-C", "/usr/local", "-xzf", archiveName}
if os.Getuid() != 0 {
args = append([]string{"sudo"}, args...)
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("cannot unpack archive: %v", err)
}
return nil
}
type tarball struct {
URL string
Version string
}
type tarballSource struct {
url, xpath string
}
var tarballSources = []tarballSource{
{"https://golang.org/dl/", "//a/@href[contains(., 'dl.google.com/go/')]"},
}
func tarballs() ([]*tarball, error) {
type result struct {
tarballs []*tarball
err error
}
results := make(chan result)
for _, source := range tarballSources {
source := source
go func() {
tbs, err := tarballsFrom(source)
results <- result{tbs, err}
}()
}
var tbs []*tarball
var err error
for _ = range tarballSources {
r := <-results
if r.err != nil {
err = r.err
} else {
tbs = append(tbs, r.tarballs...)
}
}
if err != nil {
return nil, err
}
sort.Sort(tarballSlice(tbs))
return tbs, nil
}
func tarballsFrom(source tarballSource) ([]*tarball, error) {
resp, err := http.Get(source.url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("cannot read http response: %v", err)
}
clearScripts(data)
root, err := xmlpath.ParseHTML(bytes.NewBuffer(data))
if err != nil {
return nil, err
}
var tbs []*tarball
iter := xmlpath.MustCompile(source.xpath).Iter(root)
for iter.Next() {
s := iter.Node().String()
if strings.HasPrefix(s, "//") {
s = "https:" + s
}
if strings.HasPrefix(s, "/dl/") {
s = source.url + s[4:]
}
if tb, ok := parseURL(s); ok {
tbs = append(tbs, tb)
}
}
if len(tbs) == 0 {
return nil, fmt.Errorf("no downloads available at " + source.url)
}
return tbs, nil
}
func parseURL(url string) (tb *tarball, ok bool) {
// url looks like https://.../go1.1beta2.linux-amd64.tar.gz
_, s := path.Split(url)
if len(s) < 3 || !strings.HasPrefix(s, "go") || !(s[2] >= '1' && s[2] <= '9') {
return nil, false
}
suffix := fmt.Sprintf(".linux-%s.tar.gz", getArch())
if !strings.HasSuffix(s, suffix) {
return nil, false
}
return &tarball{url, s[2 : len(s)-len(suffix)]}, true
}
func getArch() string {
arch := build.Default.GOARCH
if arch == "arm" {
return "armv6l"
}
return arch
}
func clearScripts(data []byte) {
startTag := []byte("<script")
closeTag := []byte("</script>")
var i, j int
for {
i = j + bytes.Index(data[j:], startTag)
if i < j {
break
}
i = i + bytes.IndexByte(data[i:], '>') + 1
j = i + bytes.Index(data[i:], closeTag)
for i < j {
data[i] = ' '
i++
}
}
}