This repository has been archived by the owner on Nov 13, 2021. It is now read-only.
/
artifact.go
147 lines (125 loc) · 3.56 KB
/
artifact.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
package artifact
import (
"context"
"github.com/solo-io/valet/pkg/cmd"
"io"
"net/http"
"os"
"github.com/google/go-github/github"
errors "github.com/rotisserie/eris"
)
type Downloader interface {
Download(ctx context.Context, remotePath, localPath string) error
}
var (
_ Downloader = new(githubArtifactDownloader)
CouldNotFindAssetError = errors.Errorf("Could not find asset")
)
func NewGithubDownloader(client *github.Client, repoName, tag string) *githubArtifactDownloader {
return &githubArtifactDownloader{
client: client,
repoName: repoName,
tag: tag,
}
}
type githubArtifactDownloader struct {
client *github.Client
repoName string
tag string
}
func (d *githubArtifactDownloader) Download(ctx context.Context, remotePath, localPath string) error {
release, err := d.getRelease(ctx, d.client)
if err != nil {
return err
}
asset, err := d.getAsset(ctx, release, remotePath)
if err != nil {
return err
}
return d.downloadAsset(ctx, asset, localPath)
}
func (d *githubArtifactDownloader) getRelease(ctx context.Context, client *github.Client) (*github.RepositoryRelease, error) {
release, _, err := client.Repositories.GetReleaseByTag(ctx, "solo-io", d.repoName, d.tag)
if err != nil {
cmd.Stderr().Println("Could not get release %s:%s", d.repoName, d.tag)
return nil, err
}
return release, nil
}
func (d *githubArtifactDownloader) getAsset(ctx context.Context, release *github.RepositoryRelease, remotePath string) (*github.ReleaseAsset, error) {
for _, asset := range release.Assets {
if asset.GetName() == remotePath {
return &asset, nil
}
}
cmd.Stderr().Println("Could not find asset %s:%s %s", d.repoName, d.tag, remotePath)
return nil, CouldNotFindAssetError
}
func chmod(ctx context.Context, filepath string) error {
err := os.Chmod(filepath, os.ModePerm)
if err != nil {
cmd.Stderr().Println("Error changing file permissions: %s", err.Error())
}
return err
}
func (d *githubArtifactDownloader) downloadAsset(ctx context.Context, asset *github.ReleaseAsset, filepath string) error {
cmd.Stdout().Println("Downloading asset %s to %s", asset.GetName(), filepath)
rc, redirectUrl, err := d.client.Repositories.DownloadReleaseAsset(ctx, "solo-io", d.repoName, asset.GetID())
if err != nil {
cmd.Stderr().Println("Could not download asset: %s", err.Error())
return err
}
if rc != nil {
err = copyReader(filepath, rc)
} else {
err = downloadFile(filepath, redirectUrl)
}
if err != nil {
cmd.Stderr().Println("Could not download asset: %s", err.Error())
return err
}
return chmod(ctx, filepath)
}
func copyReader(filepath string, rc io.ReadCloser) error {
defer rc.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, rc)
return err
}
func downloadFile(filepath, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
func NewUrlDownloader() *urlArtifactDownloader {
return &urlArtifactDownloader{}
}
type urlArtifactDownloader struct {
}
var _ Downloader = new(urlArtifactDownloader)
func (d *urlArtifactDownloader) Download(ctx context.Context, remotePath, localPath string) error {
cmd.Stdout().Println("Downloading file %s to %s", remotePath, localPath)
err := downloadFile(localPath, remotePath)
if err != nil {
return err
}
return chmod(ctx, localPath)
}