Skip to content

Commit

Permalink
Add inmemory.Download for downloading files without writing to disk
Browse files Browse the repository at this point in the history
this helper is put in a separate package pkg/download/inmemory to avoid
import cycles which occurs due to the network package being imported in
the pkg/download package, the cycle is network->ssh->constants->version
as a result we'll not be able to import pkg/download in pkg/crc/version

currently the helper is used for downloading release-info.json file and
the signed bundle hashes file sha256sum.txt.sig
  • Loading branch information
anjannath committed Jun 2, 2023
1 parent f3a947b commit 4308f3f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/download/inmemory/inmemory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package inmemory

import (
"fmt"
"io"
"net/http"

"github.com/cavaliergopher/grab/v3"
)

// Download takes an URL and the User-Agent string to use in the http request
// it takes http.RoundTripper as the third argument to be used by the client
func Download(url, userAgent string, transport http.RoundTripper) (io.ReadCloser, error) {
client := grab.NewClient()
client.HTTPClient = &http.Client{Transport: transport}

httpReq, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create http request: %w", err)
}
httpReq.Header.Set("User-Agent", userAgent)

grabReq, err := grab.NewRequest("", "")
if err != nil {
return nil, fmt.Errorf("failed to create grab request: %w", err)
}
grabReq.HTTPRequest = httpReq
// do not write the downloaded file to disk
grabReq.NoStore = true

rsp := client.Do(grabReq)
return rsp.Open()
}

0 comments on commit 4308f3f

Please sign in to comment.