-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.go
234 lines (206 loc) · 4.55 KB
/
package.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
package main
import (
"bytes"
"crypto/sha1"
"crypto/sha256"
"errors"
"fmt"
"hash"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
type Version struct {
Name string
Packages []*Package
}
type Package struct {
Name string
Tag string
URL string
Kind string
OS string
Arch string
Size string
released string
Checksum string
Algorithm string
}
func (p *Package) download() error {
d := NewDownloader(runtime.NumCPU(), p.Tag)
if p.URL == "" || p.Name == "" {
return errors.New(Red("Download URL is empty"))
}
return d.Download(baseUrl+p.URL, p.Name)
}
func (p *Package) checkSum() (err error) {
f, err := os.Open(SVDownload + "/" + p.Name)
if err != nil {
return err
}
defer f.Close()
var h hash.Hash
switch p.Algorithm {
case "SHA256":
h = sha256.New()
case "SHA1":
h = sha1.New()
default:
return errors.New("unsupported checksum algorithm")
}
if _, err := io.Copy(h, f); err != nil {
return err
}
if p.Checksum != fmt.Sprintf("%x", h.Sum(nil)) {
return errors.New("file checksum does not match the computed checksum")
}
return nil
}
func (p *Package) useCached() error {
return execute(p.Tag)
}
func (p *Package) useDownloaded() error {
if p.Checksum != "" {
if err := p.checkSum(); err != nil {
return err
}
}
if err := Extract(SVCache, filepath.Join(SVDownload, p.Name)); err != nil {
return err
}
PrintGreen("extract success")
if err := os.Rename(filepath.Join(SVCache, "go"), filepath.Join(SVCache, p.Tag)); err != nil {
return err
}
return p.useCached()
}
func (p *Package) useRemote() error {
if err := p.download(); err != nil {
return err
}
return p.useDownloaded()
}
// useLocal contain cached and downloaded
func (p *Package) useLocal() error {
if inCache(p.Tag) {
return p.useCached()
}
if inDownload(p.Name) {
return p.useDownloaded()
}
return errors.New(Blue("local does not exist"))
}
func (p *Package) use() (err error) {
if err := p.useLocal(); err != nil {
return p.useRemote()
}
return
}
func (p *Package) install() error {
if err := p.removeLocal(); err != nil {
return err
}
return p.useRemote()
}
func (p *Package) remove() error {
return p.removeLocal()
}
func (p *Package) removeLocal() (err error) {
tag := p.Tag
if strings.HasPrefix(tag, "v") {
tag = strings.Replace(tag, "v", "go", 1)
}
if !strings.HasPrefix(tag, "go") {
tag = "go" + tag
}
path, err := os.Readlink(SVRoot)
if err == nil && filepath.Base(path) == tag {
return errors.New(Red("[WARN]This version is in use, please use another version before uninstalling"))
}
err = os.RemoveAll(filepath.Join(SVCache, tag))
if err != nil {
return
}
return os.RemoveAll(filepath.Join(SVDownload, p.Name))
}
func (p *Package) getLocalVersion() (versions []string, err error) {
folder := filepath.Join(SVCache, "*")
versions, err = filepath.Glob(folder)
if err != nil {
return
}
for i, v := range versions {
versions[i] = filepath.Base(v)
}
return
}
func execute(tag string) (err error) {
if err = os.RemoveAll(SVRoot); err != nil {
return err
}
if err = os.Symlink(filepath.Join(SVCache, tag), SVRoot); err != nil {
return err
}
goBin := filepath.Join(SVRoot, "bin", "go")
cmd := exec.Command(goBin, "version")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
newPath := filepath.Join(SVRoot, "bin")
if p := os.Getenv("PATH"); p != "" {
newPath += string(filepath.ListSeparator) + p
}
cmd.Env = setEnv(append(os.Environ(), "GOROOT="+SVRoot, "PATH="+newPath))
if err := cmd.Run(); err != nil {
os.Exit(1)
}
return
}
// ExecCommand use shell /bin/bash -c to execute command
func ExecCommand(command string) (stdout, stderr string, err error) {
var out bytes.Buffer
var errout bytes.Buffer
cmd := exec.Command("/bin/bash", "-c", command)
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd")
}
cmd.Stdout = &out
cmd.Stderr = &errout
err = cmd.Run()
if err != nil {
stderr = string(errout.Bytes())
}
stdout = string(out.Bytes())
return
}
func inDownload(name string) bool {
path := filepath.Join(SVDownload, name)
return Exists(path)
}
func inCache(tag string) bool {
path := filepath.Join(SVCache, tag)
return Exists(path)
}
func setEnv(env []string) []string {
out := make([]string, 0, len(env))
saw := map[string]int{}
for _, kv := range env {
eq := strings.Index(kv, "=")
if eq < 1 {
out = append(out, kv)
continue
}
k := kv[:eq]
k = strings.ToLower(k)
if dupIdx, isDup := saw[k]; isDup {
out[dupIdx] = kv
} else {
saw[k] = len(out)
out = append(out, kv)
}
}
return out
}