MCPcopy Create free account
hub / github.com/cli/cli / downloadArtifact

Function downloadArtifact

pkg/cmd/run/download/http.go:31–74  ·  view source on GitHub ↗
(httpClient *http.Client, url safeurl.SafeURL, destDir safepaths.Absolute)

Source from the content-addressed store, hash-verified

29}
30
31func downloadArtifact(httpClient *http.Client, url safeurl.SafeURL, destDir safepaths.Absolute) error {
32 // TODO(api-client-rollout)
33 // This has been deferred from moving to api.Client due to streaming the artifact ZIP response body to disk instead of decoding JSON.
34 req, err := http.NewRequest("GET", url.String(), nil)
35 if err != nil {
36 return err
37 }
38 // The server rejects this :(
39 //req.Header.Set("Accept", "application/zip")
40
41 resp, err := httpClient.Do(req)
42 if err != nil {
43 return err
44 }
45 defer resp.Body.Close()
46
47 if resp.StatusCode > 299 {
48 return api.HandleHTTPError(resp)
49 }
50
51 tmpfile, err := os.CreateTemp("", "gh-artifact.*.zip")
52 if err != nil {
53 return fmt.Errorf("error initializing temporary file: %w", err)
54 }
55 defer func() {
56 _ = tmpfile.Close()
57 _ = os.Remove(tmpfile.Name())
58 }()
59
60 size, err := io.Copy(tmpfile, resp.Body)
61 if err != nil {
62 return fmt.Errorf("error writing zip archive: %w", err)
63 }
64
65 zipfile, err := zip.NewReader(tmpfile, size)
66 if err != nil {
67 return fmt.Errorf("error extracting zip archive: %w", err)
68 }
69 if err := ghzip.ExtractZip(zipfile, destDir); err != nil {
70 return fmt.Errorf("error extracting zip archive: %w", err)
71 }
72
73 return nil
74}

Callers 1

DownloadMethod · 0.85

Calls 8

HandleHTTPErrorFunction · 0.92
StringMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65
ErrorfMethod · 0.65
RemoveMethod · 0.65
NameMethod · 0.65
CopyMethod · 0.45

Tested by

no test coverage detected