MCPcopy
hub / github.com/cli/cli / downloadArtifact

Function downloadArtifact

pkg/cmd/run/download/http.go:30–71  ·  view source on GitHub ↗
(httpClient *http.Client, url string, destDir safepaths.Absolute)

Source from the content-addressed store, hash-verified

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

Callers 1

DownloadMethod · 0.85

Calls 7

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

Tested by

no test coverage detected