-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade.go
231 lines (210 loc) · 6.2 KB
/
upgrade.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
package goup
import (
"bufio"
"fmt"
"io"
"net/http"
"os/exec"
"sort"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/pkg/errors"
)
const (
RelVerURL = "https://go.googlesource.com/go/+refs"
DownloadURLWithPattern = "https://dl.google.com/go/go[version].[os]-[arch].[ext]"
)
type VersionInfo struct {
Major int
Minor int
Build int
RC bool
RCVersion int
Beta bool
BetaVersion int
}
func (vi VersionInfo) String() string {
if vi.Beta {
return fmt.Sprintf("%d.%dbeta%d", vi.Major, vi.Minor, vi.BetaVersion)
} else if vi.RC {
return fmt.Sprintf("%d.%drc%d", vi.Major, vi.Minor, vi.RCVersion)
} else if vi.Build == 0 {
return fmt.Sprintf("%d.%d", vi.Major, vi.Minor)
} else {
return fmt.Sprintf("%d.%d.%d", vi.Major, vi.Minor, vi.Build)
}
}
func DownloadUrl(version VersionInfo, os, arch string) string {
replacer := strings.NewReplacer("[version]", version.String(),
"[arch]", arch,
"[ext]", Format,
"[os]", os)
return replacer.Replace(DownloadURLWithPattern)
}
// DownloadPackage downloads Go compiled binaries from dl.google.com
// version: Go version
// arch: Go architecture
func DownloadPackage(url string, dlCallback func(totalSize int64, src io.Reader) error) (size int64, err error) {
resp, err := http.Get(url)
if err != nil {
return -1, err
}
defer resp.Body.Close()
err = dlCallback(resp.ContentLength, resp.Body)
return resp.ContentLength, err
}
// LatestVersionInfo returns all version (defined in GoogleSource) available in a slice
func LatestVersionInfo() (versionInfo []VersionInfo, err error) {
resp, err := http.Get(RelVerURL)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New("Error code: " + strconv.Itoa(resp.StatusCode))
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, err
}
verList := make([]VersionInfo, 0, 10)
doc.Find(".RefList-item").Each(func(i int, s *goquery.Selection) {
verStr := s.Find("a").Text()
if strings.HasPrefix(verStr, "go") {
verInfo, err := ExtractVersionInfo(verStr[2:])
if err == nil {
verList = append(verList, verInfo)
}
}
})
// Sort all version with latest go first
// Standard build > RC > Beta
sort.Slice(verList, func(i, j int) bool {
if verList[i].Major < verList[j].Major {
return false
}
if verList[i].Major > verList[j].Major {
return true
}
if verList[i].Minor < verList[j].Minor {
return false
}
if verList[i].Minor > verList[j].Minor {
return true
}
if verList[i].Build < verList[j].Build {
return false
}
if verList[i].Build > verList[j].Build {
return true
}
if (verList[i].Beta || verList[i].RC) && !(verList[j].RC || verList[j].Beta) {
return false
}
if !(verList[i].Beta || verList[i].RC) && (verList[j].RC || verList[j].Beta) {
return true
}
if verList[i].Beta && verList[j].RC {
return false
}
if verList[i].RC && verList[j].Beta {
return true
}
if verList[i].RCVersion < verList[j].RCVersion {
return false
}
if verList[i].RCVersion > verList[j].RCVersion {
return true
}
if verList[i].BetaVersion < verList[j].BetaVersion {
return false
}
if verList[i].BetaVersion > verList[j].BetaVersion {
return true
}
return true
})
return verList, nil
}
// LocalGoInfo returns local Go version numbers, OS and Arch
func LocalGoInfo(exePath string) (ver VersionInfo, os, arch string, err error) {
verCmd := exec.Command(exePath, "version")
out, err := verCmd.Output()
if err != nil {
return VersionInfo{}, "", "", errors.Wrap(err, "Error happens when trying to execute go")
}
if len(out) <= 0 {
return VersionInfo{}, "", "", errors.New("go version returns improper")
}
goVerInfo := strings.Split(string(out), " ")
archOSInfo := strings.Split(goVerInfo[3], "/")
verInfo, err := ExtractVersionInfo(strings.TrimSpace(goVerInfo[2][2:]))
if err != nil {
return VersionInfo{}, "", "", err
}
return verInfo, strings.TrimSpace(archOSInfo[0]), strings.TrimSpace(archOSInfo[1]), nil
}
// GoPath extract GOPATH path from `go env` command
func GoPath(exePath string) (string, error) {
verCmd := exec.Command(exePath, "env")
out, err := verCmd.Output()
if err != nil {
return "", err
}
envStr := string(out)
scanner := bufio.NewScanner(strings.NewReader(envStr))
for scanner.Scan() {
if strings.Contains(scanner.Text(), "GOROOT") {
varPair := strings.Split(scanner.Text(), "=")
return strings.Trim(varPair[1], "\""), nil
}
}
return "", errors.New("GOROOT not found")
}
func ExtractVersionInfo(version string) (versionInfo VersionInfo, err error) {
verArr := strings.Split(version, ".")
if len(verArr) != 3 && len(verArr) != 2 {
return VersionInfo{}, errors.New("Cannot parse version correctly")
}
versionInfo.Major, err = strconv.Atoi(verArr[0])
if err != nil {
return VersionInfo{}, errors.New("Cannot parse major version")
}
if strings.Contains(verArr[1], "beta") {
versionInfo.Beta = true
betaInfo := strings.Split(verArr[1], "beta")
versionInfo.Minor, err = strconv.Atoi(betaInfo[0])
if err != nil {
return VersionInfo{}, errors.New("Cannot parse minor version")
}
versionInfo.BetaVersion, err = strconv.Atoi(betaInfo[1])
if err != nil {
return VersionInfo{}, errors.New("Cannot parse minor version")
}
} else if strings.Contains(verArr[1], "rc") {
versionInfo.RC = true
rcInfo := strings.Split(verArr[1], "rc")
versionInfo.Minor, err = strconv.Atoi(rcInfo[0])
if err != nil {
return VersionInfo{}, errors.New("Cannot parse minor version")
}
versionInfo.RCVersion, err = strconv.Atoi(rcInfo[1])
if err != nil {
return VersionInfo{}, errors.New("Cannot parse minor version")
}
} else {
versionInfo.Minor, err = strconv.Atoi(verArr[1])
if err != nil {
return VersionInfo{}, errors.New("Cannot parse minor version")
}
}
if len(verArr) == 2 {
return
}
versionInfo.Build, err = strconv.Atoi(verArr[2])
if err != nil {
return VersionInfo{}, errors.New("Cannot parse build version")
}
return
}