forked from kevincobain2000/gobrew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobrew.go
401 lines (346 loc) · 10.4 KB
/
gobrew.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package gobrew
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"sort"
"strings"
"github.com/Masterminds/semver"
"github.com/kevincobain2000/gobrew/utils"
)
const (
goBrewDir string = ".gobrew"
registryPath string = "https://golang.org/dl/"
fetchTagsRepo string = "https://github.com/golang/go"
)
// Command ...
type Command interface {
ListVersions()
ListRemoteVersions()
CurrentVersion() string
Uninstall(version string)
Install(version string)
Use(version string)
Helper
}
// GoBrew struct
type GoBrew struct {
homeDir string
installDir string
versionsDir string
currentDir string
currentBinDir string
currentGoDir string
downloadsDir string
Command
}
// Helper ...
type Helper interface {
getArch() string
existsVersion(version string) bool
cleanVersionDir(version string)
mkdirs(version string)
getVersionDir(version string) string
downloadAndExtract(version string)
changeSymblinkGoBin(version string)
changeSymblinkGo(version string)
}
var gb GoBrew
// NewGoBrew instance
func NewGoBrew() GoBrew {
gb.homeDir = os.Getenv("HOME")
gb.installDir = filepath.Join(gb.homeDir, goBrewDir)
gb.versionsDir = filepath.Join(gb.installDir, "versions")
gb.currentDir = filepath.Join(gb.installDir, "current")
gb.currentBinDir = filepath.Join(gb.installDir, "current", "bin")
gb.currentGoDir = filepath.Join(gb.installDir, "current", "go")
gb.downloadsDir = filepath.Join(gb.installDir, "downloads")
return gb
}
func (gb *GoBrew) getArch() string {
return runtime.GOOS + "-" + runtime.GOARCH
}
// ListVersions that are installed by dir ls
// highlight the version that is currently symbolic linked
func (gb *GoBrew) ListVersions() {
files, err := ioutil.ReadDir(gb.versionsDir)
if err != nil {
utils.ColorError.Printf("[Error]: List versions failed: %s", err)
os.Exit(0)
}
cv := gb.CurrentVersion()
versionsSemantic := make([]*semver.Version, 0)
for _, f := range files {
v, err := semver.NewVersion(f.Name())
if err != nil {
// utils.ColorError.Printf("Error parsing version: %s", err)
} else {
versionsSemantic = append(versionsSemantic, v)
}
}
// sort semantic versions
sort.Sort(semver.Collection(versionsSemantic))
for _, versionSemantic := range versionsSemantic {
version := versionSemantic.String()
// 1.8.0 -> 1.8
reMajorVersion, _ := regexp.Compile("[0-9]+.[0-9]+.0")
if reMajorVersion.MatchString((version)) {
version = strings.Split(version, ".")[0] + "." + strings.Split(version, ".")[1]
}
if version == cv {
version = cv + "*"
utils.ColorSuccess.Println(version)
} else {
log.Println(version)
}
}
// print rc and beta versions in the end
for _, f := range files {
rcVersion := f.Name()
r, _ := regexp.Compile("beta.*|rc.*")
matches := r.FindAllString(rcVersion, -1)
if len(matches) == 1 {
if rcVersion == cv {
rcVersion = cv + "*"
utils.ColorSuccess.Println(rcVersion)
} else {
log.Println(rcVersion)
}
}
}
if cv != "" {
log.Println()
log.Printf("current: %s", cv)
}
}
// ListRemoteVersions that are installed by dir ls
func (gb *GoBrew) ListRemoteVersions() {
log.Println("[Info]: Fetching remote versions")
cmd := exec.Command(
"git",
"ls-remote",
// "--sort=version:refname",
"--tags",
fetchTagsRepo,
"go*")
output, err := cmd.CombinedOutput()
if err != nil {
utils.ColorError.Printf("[Error]: List remote versions failed: %s", err)
os.Exit(0)
}
tagsRaw := utils.BytesToString(output)
r, _ := regexp.Compile("tags/go.*")
matches := r.FindAllString(tagsRaw, -1)
versions := make([]string, len(matches))
for _, match := range matches {
versionTag := strings.ReplaceAll(match, "tags/go", "")
versions = append(versions, versionTag)
}
printGroupedVersions(versions)
}
func printGroupedVersions(versions []string) {
groupedVersions := make(map[string][]string)
for _, version := range versions {
parts := strings.Split(version, ".")
if len(parts) > 1 {
majorVersion := fmt.Sprintf("%s.%s", parts[0], parts[1])
r, _ := regexp.Compile("beta.*|rc.*")
matches := r.FindAllString(majorVersion, -1)
if len(matches) == 1 {
majorVersion = strings.Split(version, matches[0])[0]
}
groupedVersions[majorVersion] = append(groupedVersions[majorVersion], version)
}
}
// groupedVersionKeys := []string{"1", "1.1", "1.2", ..., "1.17"}
groupedVersionKeys := make([]string, 0, len(groupedVersions))
for groupedVersionKey := range groupedVersions {
groupedVersionKeys = append(groupedVersionKeys, groupedVersionKey)
}
versionsSemantic := make([]*semver.Version, 0)
for _, r := range groupedVersionKeys {
v, err := semver.NewVersion(r)
if err != nil {
// utils.ColorError.Printf("Error parsing version: %s", err)
} else {
versionsSemantic = append(versionsSemantic, v)
}
}
// sort semantic versions
sort.Sort(semver.Collection(versionsSemantic))
// match 1.0.0 or 2.0.0
reTopVersion, _ := regexp.Compile("[0-9]+.0.0")
for _, versionSemantic := range versionsSemantic {
strKey := versionSemantic.String()
lookupKey := ""
versionParts := strings.Split(strKey, ".")
// prepare lookup key for the grouped version map.
// 1.0.0 -> 1.0, 1.1.1 -> 1.1
lookupKey = versionParts[0] + "." + versionParts[1]
// On match 1.0.0, print 1. On match 2.0.0 print 2
if reTopVersion.MatchString((strKey)) {
utils.ColorMajorVersion.Print(versionParts[0])
fmt.Print("\t")
} else {
utils.ColorMajorVersion.Print(lookupKey)
fmt.Print("\t")
}
groupedVersionsSemantic := make([]*semver.Version, 0)
for _, r := range groupedVersions[lookupKey] {
v, err := semver.NewVersion(r)
if err != nil {
// utils.ColorError.Printf("Error parsing version: %s", err)
} else {
groupedVersionsSemantic = append(groupedVersionsSemantic, v)
}
}
// sort semantic versions
sort.Sort(semver.Collection(groupedVersionsSemantic))
for _, gvSemantic := range groupedVersionsSemantic {
fmt.Print(gvSemantic.String() + " ")
}
// print rc and beta versions in the end
for _, rcVersion := range groupedVersions[lookupKey] {
r, _ := regexp.Compile("beta.*|rc.*")
matches := r.FindAllString(rcVersion, -1)
if len(matches) == 1 {
fmt.Print(rcVersion + " ")
}
}
fmt.Println()
}
}
func (gb *GoBrew) existsVersion(version string) bool {
path := filepath.Join(gb.versionsDir, version, "go")
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
// CurrentVersion get current version from symb link
func (gb *GoBrew) CurrentVersion() string {
fp, err := filepath.EvalSymlinks(gb.currentBinDir)
if err != nil {
return ""
}
version := strings.ReplaceAll(fp, "/go/bin", "")
version = strings.ReplaceAll(version, gb.versionsDir, "")
version = strings.ReplaceAll(version, "/", "")
return version
}
// Uninstall the given version of go
func (gb *GoBrew) Uninstall(version string) {
if version == "" {
log.Fatal("[Error] No version provided")
}
if gb.CurrentVersion() == version {
utils.ColorError.Printf("[Error] Version: %s you are trying to remove is your current version. Please use a different version first before uninstalling the current version\n", version)
os.Exit(0)
return
}
if !gb.existsVersion(version) {
utils.ColorError.Printf("[Error] Version: %s you are trying to remove is not installed\n", version)
os.Exit(0)
}
gb.cleanVersionDir(version)
utils.ColorSuccess.Printf("[Success] Version: %s uninstalled\n", version)
}
func (gb *GoBrew) cleanVersionDir(version string) {
os.RemoveAll(gb.getVersionDir(version))
}
func (gb *GoBrew) cleanDownloadsDir() {
os.RemoveAll(gb.downloadsDir)
}
// Install the given version of go
func (gb *GoBrew) Install(version string) {
if version == "" {
log.Fatal("[Error] No version provided")
}
gb.mkdirs(version)
if gb.existsVersion(version){
utils.ColorInfo.Printf("[Info] Version: %s exists \n", version)
return
}
utils.ColorInfo.Printf("[Info] Downloading version: %s \n", version)
gb.downloadAndExtract(version)
gb.cleanDownloadsDir()
utils.ColorSuccess.Printf("[Success] Downloaded version: %s\n", version)
}
// Use a version
func (gb *GoBrew) Use(version string) {
if gb.CurrentVersion() == version {
utils.ColorInfo.Printf("[Info] Version: %s is already your current version \n", version)
return
}
utils.ColorInfo.Printf("[Info] Changing go version to: %s \n", version)
gb.changeSymblinkGoBin(version)
gb.changeSymblinkGo(version)
utils.ColorSuccess.Printf("[Success] Changed go version to: %s\n", version)
}
func (gb *GoBrew) mkdirs(version string) {
os.MkdirAll(gb.installDir, os.ModePerm)
os.MkdirAll(gb.currentDir, os.ModePerm)
os.MkdirAll(gb.versionsDir, os.ModePerm)
os.MkdirAll(gb.getVersionDir(version), os.ModePerm)
os.MkdirAll(gb.downloadsDir, os.ModePerm)
}
func (gb *GoBrew) getVersionDir(version string) string {
return filepath.Join(gb.versionsDir, version)
}
func (gb *GoBrew) downloadAndExtract(version string) {
tarName := "go" + version + "." + gb.getArch() + ".tar.gz"
downloadURL := registryPath + tarName
err := utils.Download(
downloadURL,
filepath.Join(gb.downloadsDir, tarName))
if err != nil {
gb.cleanVersionDir(version)
utils.ColorInfo.Printf("[Info]: Downloading version failed: %s \n", err)
utils.ColorError.Printf("[Error]: Please check connectivity to url: %s\n", downloadURL)
os.Exit(0)
}
cmd := exec.Command(
"tar",
"-xf",
filepath.Join(gb.downloadsDir, tarName),
"-C",
gb.getVersionDir(version))
utils.ColorInfo.Printf("[Success] Untar to %s\n", gb.getVersionDir(version))
_, err = cmd.Output()
if err != nil {
// clean up dir
gb.cleanVersionDir(version)
utils.ColorInfo.Printf("[Info]: Untar failed: %s \n", err)
utils.ColorError.Printf("[Error]: Please check if version exists from url: %s\n", downloadURL)
os.Exit(0)
}
}
func (gb *GoBrew) changeSymblinkGoBin(version string) {
goBinDst := filepath.Join(gb.versionsDir, version, "/go/bin")
os.RemoveAll(gb.currentBinDir)
cmd := exec.Command("ln", "-snf", goBinDst, gb.currentBinDir)
_, err := cmd.Output()
if err != nil {
utils.ColorError.Printf("[Error]: symbolic link failed: %s\n", err)
os.Exit(0)
}
}
func (gb *GoBrew) changeSymblinkGo(version string) {
os.RemoveAll(gb.currentGoDir)
versionGoDir := filepath.Join(gb.versionsDir, gb.CurrentVersion(), "go")
cmd := exec.Command("ln", "-snf", versionGoDir, gb.currentGoDir)
_, err := cmd.Output()
if err != nil {
utils.ColorError.Printf("[Error]: symbolic link failed: %s\n", err)
os.Exit(0)
}
}