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

Function downloadAsset

pkg/cmd/extension/http.go:81–112  ·  view source on GitHub ↗

downloadAsset downloads a single asset to the given file path.

(httpClient *http.Client, assetURL safeurl.SafeURL, destPath string)

Source from the content-addressed store, hash-verified

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

Callers 1

installBinMethod · 0.70

Calls 6

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

Tested by

no test coverage detected