-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
89 lines (73 loc) · 2.74 KB
/
get.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
package getrelease
import (
"github.com/hashicorp/go-getter"
"io"
"net/http"
"regexp"
)
// Client is one of github, gitlab connection that allows for fetching assets from releases
type Client interface {
getLatestAssetUrl(*regexp.Regexp, string, string) (*http.Client, *string, *string, error)
getTagAssetUrl(*regexp.Regexp, string, string, string) (*http.Client, *string, *string, error)
}
// ProgressTracker allows to track the progress of downloads.
type ProgressTracker interface {
TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser)
}
// urlGetter is a type that returns url fetched for the asset
type urlGetter = func(assetNameReg *regexp.Regexp) (*http.Client, *string, *string, error)
// get is a generic asset fetcher and downloader
func get(dst string, assetNameReg *regexp.Regexp, urlGetter urlGetter, opts ...Options) (string, error) {
config := &Configuration{}
if err := config.configure(opts...); err != nil {
return "", err
}
_, assetName, retUrl, err := urlGetter(assetNameReg)
if err != nil {
return "", err
}
url, err := adjustUrlForGetter(*retUrl, urlGetter, config)
if err != nil {
return "", err
}
if err := getter.GetAny(dst, url, func(client *getter.Client) error {
client.ProgressListener = config.ProgressTracker
client.Pwd = config.Pwd
return nil
}); err != nil {
return "", err
}
return *assetName, nil
}
// GetTagAsset fetches provided tag release and matches the provided asset name regex with all the assets.
// If asset is found, it is downloaded in provided dst otherwise error is thrown
func GetTagAsset(client Client, dst, assetNameReg, owner, repo, tag string, opts ...Options) (string, error) {
reg, err := regexp.Compile(assetNameReg)
if err != nil {
return "", err
}
urlGetter := func(assetNameReg *regexp.Regexp) (*http.Client, *string, *string, error) {
client, assetName, url, err := client.getTagAssetUrl(assetNameReg, owner, repo, tag)
if err != nil {
return nil, nil, nil, err
}
return client, assetName, url, nil
}
return get(dst, reg, urlGetter, opts...)
}
// GetLatestAsset fetches latest release and matches the provided asset name regex with all the assets.
// If asset is found, it is downloaded in provided dst otherwise error is thrown
func GetLatestAsset(client Client, dst, assetNameReg, owner, repo string, opts ...Options) (string, error) {
reg, err := regexp.Compile(assetNameReg)
if err != nil {
return "", err
}
urlGetter := func(assetNameReg *regexp.Regexp) (*http.Client, *string, *string, error) {
client, assetName, url, err := client.getLatestAssetUrl(assetNameReg, owner, repo)
if err != nil {
return nil, nil, nil, err
}
return client, assetName, url, nil
}
return get(dst, reg, urlGetter, opts...)
}