-
Notifications
You must be signed in to change notification settings - Fork 24
/
gvm.go
299 lines (244 loc) · 6.44 KB
/
gvm.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
package gvm
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/andrewkroh/gvm/common"
)
type AvailableVersion struct {
Version *GoVersion
Source bool // Available to install from source.
Binary bool // Available to download as a binary.
}
func (av AvailableVersion) String() string {
switch {
case av.Source && av.Binary:
return fmt.Sprintf("%v\t(source, binary)", av.Version)
case av.Source:
return fmt.Sprintf("%v\t(source)", av.Version)
case av.Binary:
return fmt.Sprintf("%v\t(binary)", av.Version)
default:
return av.Version.String()
}
}
type Manager struct {
// GVM Home directory. Defaults to $HOME/.gvm
Home string
// GOOS settings. Defaults to current OS.
GOOS string
// GOARCH setting. Defaults to the current architecture.
GOARCH string
// Golang binary store URL. Used to download listing and go binaries.
// Defaults to https://storage.googleapis.com/golang
GoStorageHome string
// GoSourceURL configres the update git repository to download and update local
// source checkouts from.
// Defaults to https://go.googlesource.com/go
GoSourceURL string
HTTPTimeout time.Duration
Logger logrus.FieldLogger
cacheDir string
versionsDir string
logsDir string
}
func (m *Manager) Init() error {
if m.Home == "" {
home, err := homeDir()
if err != nil {
return err
}
m.Home = filepath.Join(home, ".gvm")
}
if m.GoStorageHome == "" {
m.GoStorageHome = "https://storage.googleapis.com/golang"
}
if m.GoSourceURL == "" {
m.GoSourceURL = "https://go.googlesource.com/go"
}
if m.HTTPTimeout == 0 {
m.HTTPTimeout = 3 * time.Minute
}
if m.GOOS == "" {
m.GOOS = runtime.GOOS
}
if m.GOARCH == "" {
switch runtime.GOARCH {
default:
m.GOARCH = runtime.GOARCH
case "arm":
// The only binary releases are for ARM v6.
m.GOARCH = "armv6l"
}
}
if m.Logger == nil {
m.Logger = logrus.StandardLogger()
}
m.cacheDir = filepath.Join(m.Home, "cache")
m.versionsDir = filepath.Join(m.Home, "versions")
m.logsDir = filepath.Join(m.Home, "logs")
return m.ensureDirStruct()
}
func (m *Manager) UpdateCache() error {
return m.updateSrcCache()
}
func (m *Manager) ensureDirStruct() error {
for _, dir := range []string{m.cacheDir, m.versionsDir, m.logsDir} {
if err := os.MkdirAll(dir, os.ModeDir|0o755); err != nil {
return err
}
}
return nil
}
func (m *Manager) Available() ([]AvailableVersion, error) {
if !m.hasSrcCache() {
versions, err := m.AvailableBinaries()
if err != nil {
return nil, err
}
available := make([]AvailableVersion, 0, len(versions))
for _, ver := range versions {
available = append(available, AvailableVersion{Version: ver, Binary: true})
}
return available, nil
}
src, err := m.AvailableSource()
if err != nil {
return nil, err
}
versionSet := make(map[string]*AvailableVersion, len(src))
for _, ver := range src {
versionSet[ver.String()] = &AvailableVersion{Version: ver, Source: true}
}
toSlice := func() []AvailableVersion {
available := make([]AvailableVersion, 0, len(versionSet))
for _, ver := range versionSet {
available = append(available, *ver)
}
sort.Slice(available, func(i, j int) bool {
return available[i].Version.LessThan(available[j].Version)
})
return available
}
bin, err := m.AvailableBinaries()
if err != nil {
// Return the source versions if we cannot get binary info.
m.Logger.WithError(err).Info("Failed to list available binary versions.")
return toSlice(), nil
}
// Merge source and binary versions.
for _, ver := range bin {
if avail, found := versionSet[ver.String()]; found {
avail.Binary = true
continue
}
versionSet[ver.String()] = &AvailableVersion{Version: ver, Binary: true}
}
return toSlice(), nil
}
func (m *Manager) Remove(version *GoVersion) error {
dir := m.VersionGoROOT(version)
fi, err := os.Stat(dir)
if os.IsNotExist(err) {
return fmt.Errorf("version %q not installed", version)
}
if !fi.IsDir() {
return fmt.Errorf("path %q is not a directory", dir)
}
return os.RemoveAll(dir)
}
// Installed returns all installed go version
func (m *Manager) Installed() ([]*GoVersion, error) {
files, err := os.ReadDir(m.versionsDir)
if err != nil {
return nil, err
}
versionSuffix := fmt.Sprintf(".%v.%v", m.GOOS, m.GOARCH)
list := make([]*GoVersion, 0, len(files))
for _, fi := range files {
name := fi.Name()
name = strings.TrimSuffix(name, versionSuffix)
name = strings.TrimPrefix(name, "go")
v, err := ParseVersion(name)
if err != nil {
continue
}
list = append(list, v)
}
sortVersions(list)
return list, nil
}
// HasVersion checks if a given go version is installed
func (m *Manager) HasVersion(version *GoVersion) (bool, error) {
return existsDir(m.VersionGoROOT(version))
}
// VersionGoROOT returns the GOROOT path for a go version. VersionGoROOT does
// not check if the version is installed.
func (m *Manager) VersionGoROOT(version *GoVersion) string {
return filepath.Join(m.versionsDir, m.versionDir(version))
}
func (m *Manager) versionDir(version *GoVersion) string {
return fmt.Sprintf("go%v.%v.%v", version, m.GOOS, m.GOARCH)
}
func (m *Manager) Build(version *GoVersion) (string, error) {
if version.IsTip() {
return m.ensureUpToDateTip()
}
has, err := m.HasVersion(version)
if err != nil {
return "", err
}
if has {
return m.VersionGoROOT(version), nil
}
return m.installSrc(version)
}
func (m *Manager) Install(version *GoVersion) (string, error) {
if version.IsTip() {
return m.ensureUpToDateTip()
}
has, err := m.HasVersion(version)
if err != nil {
return "", err
}
if has {
return m.VersionGoROOT(version), nil
}
if tryBinary := !version.IsTip(); tryBinary {
dir, err := m.installBinary(version)
if err == nil {
return dir, nil
}
// Only continue to installing from source if the server confirms 404.
if !errors.Is(err, common.ErrNotFound) {
return "", err
}
m.Logger.Debug("Binary release not found on server. Trying to install from source.")
}
return m.installSrc(version)
}
func (m *Manager) ensureUpToDateTip() (string, error) {
version, _ := ParseVersion("tip")
has, err := m.HasVersion(version)
if err != nil {
return "", err
}
// no updates since last build -> return installed version
if has {
updates, err := m.tryRefreshSrcCache()
if err != nil {
return "", err
}
if !updates {
return m.VersionGoROOT(version), nil
}
}
// new updates in cache -> rebuild
return m.installSrc(version)
}