This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
/
discovery_download.go
209 lines (184 loc) · 4.89 KB
/
discovery_download.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
package sdk
import (
"archive/tar"
"archive/zip"
"fmt"
"github.com/blang/semver"
"github.com/echocat/mageplus/http"
mio "github.com/echocat/mageplus/io"
"github.com/mholt/archiver/v3"
"io"
"log"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
)
const DefaultVersion = "1.14"
const EnvVersion = "GO_VERSION"
var errLog = log.New(os.Stderr, "", 0)
var infoLog = log.New(os.Stderr, "", 0)
type DownloadDiscovery struct {
Version semver.Version
Os string
Arch string
}
func NewDownloadDiscovery(version string) (*DownloadDiscovery, error) {
parsedVersion, err := semver.ParseTolerant(version)
if err != nil {
return nil, nil
}
return &DownloadDiscovery{
Version: parsedVersion,
Os: runtime.GOOS,
Arch: runtime.GOARCH,
}, nil
}
func MustNewDownloadDiscovery(version string) *DownloadDiscovery {
instance, err := NewDownloadDiscovery(version)
if err != nil {
errLog.Fatalln("Error:", err)
}
return instance
}
func NewDefaultDownloadDiscovery() *DownloadDiscovery {
if v, ok := os.LookupEnv(EnvVersion); ok {
return MustNewDownloadDiscovery(v)
}
return MustNewDownloadDiscovery(DefaultVersion)
}
func (instance DownloadDiscovery) Discover() ([]Sdk, error) {
candidate, err := instance.ToSdk()
if err != nil {
return nil, err
}
if err := candidate.Validate(); err == nil {
return []Sdk{candidate}, nil
} else if err == ErrSdkDifferent {
// Continue to download it...
} else {
return nil, err
}
downloadUrl, err := instance.DownloadUrl()
if err != nil {
return nil, err
}
infoLog.Printf("Downloading Golang SDK from %s...", downloadUrl)
if err := http.Execute(downloadUrl,
http.WriteToTemporaryFile("", instance.String(), func(input *os.File) error {
return instance.extract(input.Name(), candidate)
}),
); err != nil {
return nil, err
}
return []Sdk{candidate}, nil
}
func (instance DownloadDiscovery) extract(input string, to Sdk) error {
return instance.walker().Walk(input, func(candidate archiver.File) error {
return instance.extractFile(candidate, to)
})
}
func (instance DownloadDiscovery) extractFile(candidate archiver.File, to Sdk) error {
var name string
if h, ok := candidate.Header.(zip.FileHeader); ok {
name = h.Name
} else if h, ok := candidate.Header.(*zip.FileHeader); ok {
name = h.Name
} else if h, ok := candidate.Header.(tar.Header); ok {
name = h.Name
} else if h, ok := candidate.Header.(*tar.Header); ok {
name = h.Name
} else {
return fmt.Errorf("unexpected header type: %v", reflect.TypeOf(candidate.Header))
}
if strings.HasPrefix(name, "go/") {
name = name[3:]
}
if name == "" {
return nil
}
target := filepath.Join(to.Root, name)
if candidate.IsDir() {
if err := os.MkdirAll(target, candidate.Mode()); err != nil {
return fmt.Errorf("cannot extract '%s': %v", name, err)
}
return nil
}
if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
return fmt.Errorf("cannot create parent of '%s': %v", name, err)
}
w, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, candidate.Mode())
if err != nil {
return fmt.Errorf("cannot extract '%s': %v", name, err)
}
defer mio.CloseQuietly(w)
_, err = io.Copy(w, candidate)
if err != nil {
return fmt.Errorf("cannot extract '%s': %v", name, err)
}
return nil
}
func (instance DownloadDiscovery) walker() archiver.Walker {
if instance.Os == "windows" {
return archiver.NewZip()
}
return archiver.NewTarGz()
}
func (instance DownloadDiscovery) ToSdk() (Sdk, error) {
targetPath, err := instance.TargetPath()
if err != nil {
return Sdk{}, err
}
ext := ""
if instance.Os == "windows" {
ext = ".exe"
}
return Sdk{
Version: instance.Version,
Os: instance.Os,
Arch: instance.Arch,
Root: targetPath,
GoBinary: filepath.Join(targetPath, "bin", "go"+ext),
}, nil
}
func (instance DownloadDiscovery) TargetPath() (string, error) {
gopath, err := instance.Gopath()
if err != nil {
return "", fmt.Errorf("cannot determine sdk target directory: %v", err)
}
return filepath.Join(gopath, "pkg", "sdk", instance.String()), nil
}
func (instance DownloadDiscovery) String() string {
return fmt.Sprintf("%s.%s-%s",
instance.VersionString(),
instance.Os,
instance.Arch,
)
}
func (instance DownloadDiscovery) Gopath() (string, error) {
if v, ok := os.LookupEnv("GOPATH"); ok {
return v, nil
}
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(homeDir, ".go"), nil
}
func (instance DownloadDiscovery) DownloadUrl() (string, error) {
ext := "tar.gz"
if instance.Os == "windows" {
ext = "zip"
}
return fmt.Sprintf("https://dl.google.com/go/go%s.%s",
instance.String(),
ext,
), nil
}
func (instance DownloadDiscovery) VersionString() string {
if instance.Version.Patch == 0 {
return fmt.Sprintf("%d.%d", instance.Version.Major, instance.Version.Minor)
}
return fmt.Sprintf("%d.%d.%d", instance.Version.Major, instance.Version.Minor, instance.Version.Patch)
}