-
Notifications
You must be signed in to change notification settings - Fork 58
/
in_command.go
227 lines (192 loc) · 5.07 KB
/
in_command.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
package resource
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"github.com/google/go-github/v66/github"
)
type InCommand struct {
github GitHub
writer io.Writer
}
func NewInCommand(github GitHub, writer io.Writer) *InCommand {
return &InCommand{
github: github,
writer: writer,
}
}
func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
err := os.MkdirAll(destDir, 0755)
if err != nil {
return InResponse{}, err
}
var foundRelease *github.RepositoryRelease
var commitSHA string
id, _ := strconv.Atoi(request.Version.ID)
foundRelease, err = c.github.GetRelease(id)
if err != nil {
foundRelease, err = c.github.GetReleaseByTag(request.Version.Tag)
if err != nil {
return InResponse{}, err
}
}
if foundRelease == nil {
return InResponse{}, errors.New("no releases")
}
if foundRelease.HTMLURL != nil && *foundRelease.HTMLURL != "" {
urlPath := filepath.Join(destDir, "url")
err = ioutil.WriteFile(urlPath, []byte(*foundRelease.HTMLURL), 0644)
if err != nil {
return InResponse{}, err
}
}
if foundRelease.TagName != nil && *foundRelease.TagName != "" {
tagPath := filepath.Join(destDir, "tag")
err = ioutil.WriteFile(tagPath, []byte(*foundRelease.TagName), 0644)
if err != nil {
return InResponse{}, err
}
versionParser, err := newVersionParser(request.Source.TagFilter)
if err != nil {
return InResponse{}, err
}
version := versionParser.parse(*foundRelease.TagName)
versionPath := filepath.Join(destDir, "version")
err = ioutil.WriteFile(versionPath, []byte(version), 0644)
if err != nil {
return InResponse{}, err
}
if foundRelease.Draft != nil && !*foundRelease.Draft {
commitPath := filepath.Join(destDir, "commit_sha")
commitSHA, err = c.github.ResolveTagToCommitSHA(*foundRelease.TagName)
if err != nil {
return InResponse{}, err
}
if commitSHA != "" {
err = ioutil.WriteFile(commitPath, []byte(commitSHA), 0644)
if err != nil {
return InResponse{}, err
}
}
}
if foundRelease.Body != nil && *foundRelease.Body != "" {
body := *foundRelease.Body
bodyPath := filepath.Join(destDir, "body")
err = ioutil.WriteFile(bodyPath, []byte(body), 0644)
if err != nil {
return InResponse{}, err
}
}
if foundRelease.PublishedAt != nil || foundRelease.CreatedAt != nil {
timestampPath := filepath.Join(destDir, "timestamp")
timestamp, err := getTimestamp(foundRelease).MarshalText()
if err != nil {
return InResponse{}, err
}
err = ioutil.WriteFile(timestampPath, timestamp, 0644)
if err != nil {
return InResponse{}, err
}
}
}
assets, err := c.github.ListReleaseAssets(*foundRelease)
if err != nil {
return InResponse{}, err
}
for _, asset := range assets {
state := asset.State
if state == nil || *state != "uploaded" {
continue
}
path := filepath.Join(destDir, *asset.Name)
var matchFound bool
if len(request.Params.Globs) == 0 {
matchFound = true
} else {
for _, glob := range request.Params.Globs {
matches, err := filepath.Match(glob, *asset.Name)
if err != nil {
return InResponse{}, err
}
if matches {
matchFound = true
break
}
}
}
if !matchFound {
continue
}
fmt.Fprintf(c.writer, "downloading asset: %s\n", *asset.Name)
err := c.downloadAsset(asset, path)
if err != nil {
return InResponse{}, err
}
}
if request.Params.IncludeSourceTarball && foundRelease.TagName != nil {
u, err := c.github.GetTarballLink(*foundRelease.TagName)
if err != nil {
return InResponse{}, err
}
fmt.Fprintln(c.writer, "downloading source tarball to source.tar.gz")
if err := c.downloadFile(u.String(), filepath.Join(destDir, "source.tar.gz")); err != nil {
return InResponse{}, err
}
}
if request.Params.IncludeSourceZip && foundRelease.TagName != nil {
u, err := c.github.GetZipballLink(*foundRelease.TagName)
if err != nil {
return InResponse{}, err
}
fmt.Fprintln(c.writer, "downloading source zip to source.zip")
if err := c.downloadFile(u.String(), filepath.Join(destDir, "source.zip")); err != nil {
return InResponse{}, err
}
}
return InResponse{
Version: versionFromRelease(foundRelease),
Metadata: metadataFromRelease(foundRelease, commitSHA),
}, nil
}
func (c *InCommand) downloadAsset(asset *github.ReleaseAsset, destPath string) error {
out, err := os.Create(destPath)
if err != nil {
return err
}
defer out.Close()
content, err := c.github.DownloadReleaseAsset(*asset)
if err != nil {
return err
}
defer content.Close()
_, err = io.Copy(out, content)
if err != nil {
return err
}
return nil
}
func (c *InCommand) downloadFile(url, destPath string) error {
out, err := os.Create(destPath)
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download file `%s`: HTTP status %d", filepath.Base(destPath), resp.StatusCode)
}
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}