-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
199 lines (173 loc) · 4.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
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
package main
import (
"flag"
"log"
"os"
"strconv"
"strings"
"github.com/kevincobain2000/gobrew"
"github.com/kevincobain2000/gobrew/utils"
)
var args []string
var actionArg = ""
var versionArg = ""
var version = "dev"
var allowedArgs = []string{
"h",
"help",
"ls",
"list",
"ls-remote",
"install",
"use",
"uninstall",
"interactive",
"noninteractive",
"prune",
"version",
"self-update",
}
func init() {
log.SetFlags(0)
if !isArgAllowed() {
log.Println("[Info] Invalid usage")
log.Print(usage())
return
}
flag.Parse()
args = flag.Args()
if len(args) == 0 {
actionArg = "interactive"
} else {
actionArg = args[0]
}
if len(args) == 2 {
versionArg = args[1]
versionArgSlice := strings.Split(versionArg, ".")
if len(versionArgSlice) == 3 {
majorVersionNum, _ := strconv.Atoi(versionArgSlice[1])
// Comply with: https://github.com/kevincobain2000/gobrew/issues/113
if versionArgSlice[2] == "0" && majorVersionNum < 21 {
// Keep complying with https://github.com/kevincobain2000/gobrew/pull/24
versionArg = versionArgSlice[0] + "." + versionArgSlice[1]
}
}
if len(versionArgSlice) == 2 {
majorVersionNum, _ := strconv.Atoi(versionArgSlice[0])
minorVersionNum, _ := strconv.Atoi(versionArgSlice[1])
// Comply with: https://github.com/kevincobain2000/gobrew/issues/156
// Check if the major version is 1 and the minor version is 21 or greater
if majorVersionNum == 1 && minorVersionNum >= 21 {
// Modify the versionArg to include ".0"
versionArg += ".0"
}
}
}
}
func main() {
rootDir := os.Getenv("GOBREW_ROOT")
if rootDir == "" {
var err error
rootDir, err = os.UserHomeDir()
utils.CheckError(err, "failed get home directory and GOBREW_ROOT not defined")
}
registryPath := gobrew.DefaultRegistryPath
if p := os.Getenv("GOBREW_REGISTRY"); p != "" {
registryPath = p
}
config := gobrew.Config{
RootDir: rootDir,
RegistryPathURL: registryPath,
GobrewDownloadURL: gobrew.DownloadURL,
GobrewTags: gobrew.TagsAPI,
GobrewVersionsURL: gobrew.VersionsURL,
}
gb := gobrew.NewGoBrew(config)
switch actionArg {
case "interactive", "info":
gb.Interactive(true)
case "noninteractive":
gb.Interactive(false)
case "h", "help":
log.Print(usage())
case "ls", "list":
gb.ListVersions()
case "ls-remote":
gb.ListRemoteVersions(true)
case "install":
gb.Install(versionArg)
if gb.CurrentVersion() == gobrew.NoneVersion {
gb.Use(versionArg)
}
case "use":
gb.Use(versionArg)
case "uninstall":
gb.Uninstall(versionArg)
case "prune":
gb.Prune()
case "version":
gb.Version(version)
case "self-update":
gb.Upgrade(version)
}
}
func isArgAllowed() bool {
ok := true
if len(os.Args) > 1 {
_, ok = Find(allowedArgs, os.Args[1])
if !ok {
return false
}
}
if len(os.Args) > 2 {
_, ok = Find(allowedArgs, os.Args[1])
if !ok {
return false
}
}
return ok
}
// Find takes a slice and looks for an element in it. If found it will
// return it's key, otherwise it will return -1 and a bool of false.
func Find(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
func usage() string {
usageMsg :=
`
# Add gobrew to your ~/.bashrc or ~/.zshrc
export PATH="$HOME/.gobrew/current/bin:$HOME/.gobrew/bin:$PATH"
export GOROOT="$HOME/.gobrew/current/go"
`
msg := `
gobrew ` + version + `
Usage:
gobrew use <version> Install and set <version>
gobrew ls Alias for list
gobrew ls-remote List remote versions (including rc|beta versions)
gobrew install <version> Only install <version> (binary from official or GOBREW_REGISTRY env)
gobrew uninstall <version> Uninstall <version>
gobrew list List installed versions
gobrew self-update Self update this tool
gobrew prune Uninstall all go versions except current version
gobrew version Show gobrew version
gobrew help Show this message
Examples:
gobrew use 1.16 # use go version 1.16
gobrew use 1.16.1 # use go version 1.16.1
gobrew use 1.16rc1 # use go version 1.16rc1
gobrew use 1.16@latest # use go version latest of 1.16
gobrew use 1.16@dev-latest # use go version latest of 1.16, including rc and beta
# Note: rc and beta become no longer latest upon major release
gobrew use mod # use go version listed in the go.mod file
gobrew use latest # use go version latest available
gobrew use dev-latest # use go version latest avalable, including rc and beta
Installation Path:
` + usageMsg
return msg
}