forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
70 lines (58 loc) · 1.48 KB
/
http.go
File metadata and controls
70 lines (58 loc) · 1.48 KB
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
package download
import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/run/shared"
)
type apiPlatform struct {
client *http.Client
repo ghrepo.Interface
}
func (p *apiPlatform) List(runID string) ([]shared.Artifact, error) {
return shared.ListArtifacts(p.client, p.repo, runID)
}
func (p *apiPlatform) Download(url string, dir string) error {
return downloadArtifact(p.client, url, dir)
}
func downloadArtifact(httpClient *http.Client, url, destDir string) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
// The server rejects this :(
//req.Header.Set("Accept", "application/zip")
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode > 299 {
return api.HandleHTTPError(resp)
}
tmpfile, err := ioutil.TempFile("", "gh-artifact.*.zip")
if err != nil {
return fmt.Errorf("error initializing temporary file: %w", err)
}
defer func() {
_ = tmpfile.Close()
_ = os.Remove(tmpfile.Name())
}()
size, err := io.Copy(tmpfile, resp.Body)
if err != nil {
return fmt.Errorf("error writing zip archive: %w", err)
}
zipfile, err := zip.NewReader(tmpfile, size)
if err != nil {
return fmt.Errorf("error extracting zip archive: %w", err)
}
if err := extractZip(zipfile, destDir); err != nil {
return fmt.Errorf("error extracting zip archive: %w", err)
}
return nil
}