MCPcopy Index your code
hub / github.com/cli/cli / downloadAsset

Function downloadAsset

pkg/cmd/extension/http.go:77–108  ·  view source on GitHub ↗

downloadAsset downloads a single asset to the given file path.

(httpClient *http.Client, asset releaseAsset, destPath string)

Source from the content-addressed store, hash-verified

75
76// downloadAsset downloads a single asset to the given file path.
77func downloadAsset(httpClient *http.Client, asset releaseAsset, destPath string) (downloadErr error) {
78 var req *http.Request
79 if req, downloadErr = http.NewRequest("GET", asset.APIURL, nil); downloadErr != nil {
80 return
81 }
82
83 req.Header.Set("Accept", "application/octet-stream")
84
85 var resp *http.Response
86 if resp, downloadErr = httpClient.Do(req); downloadErr != nil {
87 return
88 }
89 defer resp.Body.Close()
90
91 if resp.StatusCode > 299 {
92 downloadErr = api.HandleHTTPError(resp)
93 return
94 }
95
96 var f *os.File
97 if f, downloadErr = os.OpenFile(destPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755); downloadErr != nil {
98 return
99 }
100 defer func() {
101 if err := f.Close(); downloadErr == nil && err != nil {
102 downloadErr = err
103 }
104 }()
105
106 _, downloadErr = io.Copy(f, resp.Body)
107 return
108}
109
110var commitNotFoundErr = errors.New("commit not found")
111var releaseNotFoundErr = errors.New("release not found")

Callers 1

installBinMethod · 0.70

Calls 5

HandleHTTPErrorFunction · 0.92
SetMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65
CopyMethod · 0.45

Tested by

no test coverage detected