-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
204 lines (165 loc) · 4.23 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
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"runtime"
"slices"
"sort"
"strconv"
"strings"
)
const fileName string = "go.tar.gz"
const oldGoBakDir string = "/usr/local/go-bak"
func main() {
versions, err := getVersions()
if err != nil {
fmt.Println("failed to get versions: ", err)
os.Exit(1)
}
fmt.Println("Current version: ", strings.TrimPrefix(runtime.Version(), "go"))
fmt.Println("Versions:", strings.Join(versions, ", "))
version := getVersion()
if !slices.Contains(versions, version) {
fmt.Println("Invalid version selected: ", version)
os.Exit(1)
}
url := fmt.Sprintf("https://go.dev/dl/go%s.linux-amd64.tar.gz", version)
f, err := os.Create(fileName)
if err != nil {
fmt.Println("failed to create the output file: ", err)
os.Exit(2)
}
defer func(f *os.File) {
if _, err := os.Stat(fileName); errors.Is(err, os.ErrNotExist) {
return
}
if err := f.Close(); err != nil {
fmt.Println("failed to close the file: ", err)
}
}(f)
if err := downloadFile(f, url); err != nil {
fmt.Println("failed to download the file: ", err)
return
}
if err := moveOldVersion(); err != nil {
fmt.Println("failed to move the old version: ", err)
return
}
if err := installNewVersion(); err != nil {
fmt.Println("failed to install the new version: ", err)
return
}
if err := os.Remove(fileName); err != nil {
fmt.Println("failed to remove the file: ", err)
return
}
if err := removeOldVersionBak(); err != nil {
fmt.Println("failed to remove old version: ", err)
return
}
fmt.Println("Done. Please run go version to confirm correct installation.")
}
func downloadFile(file io.Writer, url string) error {
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("failed to get the file: %w", err)
}
defer func(Body io.ReadCloser) {
if err := Body.Close(); err != nil {
fmt.Println("failed to close the response body: ", err)
}
}(resp.Body)
_, err = io.Copy(file, resp.Body)
if err != nil {
return fmt.Errorf("failed to write to the file: %w", err)
}
return nil
}
func getVersions() ([]string, error) {
type release struct {
Version string `json:"version"`
Stable bool `json:"stable"`
}
res, err := http.Get("https://go.dev/dl/?mode=json&include=all")
if err != nil {
return nil, fmt.Errorf("failed to get the tags: %w", err)
}
defer func(Body io.ReadCloser) {
if err := Body.Close(); err != nil {
fmt.Println("failed to close the response body: ", err)
}
}(res.Body)
var releases []release
if err := json.NewDecoder(res.Body).Decode(&releases); err != nil {
return nil, fmt.Errorf("failed to parse the body: %w", err)
}
versions := make([]string, 0)
for _, r := range releases {
if r.Stable {
versions = append(versions, strings.TrimPrefix(r.Version, "go"))
}
}
sort.Slice(versions, func(i, j int) bool {
parts1 := strings.Split(versions[i], ".")
parts2 := strings.Split(versions[j], ".")
for k := 0; k < len(parts1) && k < len(parts2); k++ {
if parts1[k] != parts2[k] {
p1, _ := strconv.Atoi(parts1[k])
p2, _ := strconv.Atoi(parts2[k])
return p1 > p2
}
}
return len(parts1) > len(parts2)
})
return versions, nil
}
func getVersion() string {
var v string
r := bufio.NewReader(os.Stdin)
for {
_, err := fmt.Fprint(os.Stdout, "Which version?\n")
if err != nil {
fmt.Println("failed to write to stdout: ", err)
os.Exit(1)
}
v, _ = r.ReadString('\n')
v = strings.TrimSpace(v)
if v != "" {
break
}
}
return v
}
func installNewVersion() error {
tar := exec.Command("tar", "-C", "/usr/local", "-xzf", fileName)
_, err := tar.Output()
if err != nil {
return fmt.Errorf("failed to execute tar: %w", err)
}
fmt.Println("Extracted the new version.")
return nil
}
func moveOldVersion() error {
mv := exec.Command("mv", "/usr/local/go", oldGoBakDir)
_, err := mv.Output()
if err != nil {
return fmt.Errorf("failed to execute mv: %w", err)
}
fmt.Println("Moved previous installation to: ", oldGoBakDir)
return nil
}
func removeOldVersionBak() error {
rm := exec.Command("rm", "-rf", oldGoBakDir)
_, err := rm.Output()
if err != nil {
return fmt.Errorf("failed to execute rm: %w", err)
}
fmt.Println("Removed previous installation.")
return nil
}