-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlatest.go
78 lines (68 loc) · 1.85 KB
/
latest.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
package releases
import (
"context"
"fmt"
"runtime"
"strings"
"time"
"github.com/circleci/ex/releases/download"
)
// DownloadConfig is the configuration for the DownloadLatest helper
type DownloadConfig struct {
// BaseURL is the url of the file binary release file server
BaseURL string
// Which is the application to download
Which string
// Binary is the binary to download. If empty it will default to the value of Which
Binary string
// Pinned if set will use this version to download
Pinned string
// Dir is the directory to download into, if empty will default to ../bin
Dir string
}
// DownloadLatest is a helper that will download the latest test binary
func DownloadLatest(ctx context.Context, conf DownloadConfig) (string, error) {
d := New(conf.BaseURL + "/" + conf.Which)
var ver string
if conf.Pinned != "" {
ver = conf.Pinned
} else {
var err error
ver, err = d.Version(ctx)
if err != nil {
return "", fmt.Errorf("version failed: %w", err)
}
}
testBinURLs, err := d.ResolveURLs(ctx, Requirements{
Version: ver,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
})
if err != nil {
return "", fmt.Errorf("resolve failed: %w", err)
}
if conf.Binary == "" {
conf.Binary = conf.Which
}
testBinURL, ok := testBinURLs[conf.Binary]
if !ok {
return "", fmt.Errorf("resolve binary failed: %s", conf.Binary)
}
// default the download directory to bin
if conf.Dir == "" {
conf.Dir = "../bin"
}
dl, err := download.NewDownloader(time.Minute, conf.Dir)
if err != nil {
return "", fmt.Errorf("download failed: %w", err)
}
path, err := dl.Download(ctx, testBinURL, 0700)
if err != nil {
return "", fmt.Errorf("download (%s) problem: %w", testBinURL, err)
}
const winExeExtension = ".exe"
if runtime.GOOS == "windows" && !strings.HasSuffix(path, winExeExtension) {
path += winExeExtension
}
return path, nil
}