Skip to content

Commit

Permalink
enhancement: add support for downloading from blob storage instead of…
Browse files Browse the repository at this point in the history
… http endpoint
  • Loading branch information
mstg committed Apr 3, 2022
1 parent 8f00773 commit 8f55eb3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
8 changes: 3 additions & 5 deletions cmd/srpmproc/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var cdnUrl string
func init() {
fetch.Flags().StringVar(&cdnUrl, "cdn-url", "", "Path to CDN")
_ = fetch.MarkFlagRequired("cdn-url")

root.AddCommand(fetch)
}

func runFetch(_ *cobra.Command, _ []string) {
Expand All @@ -45,12 +47,8 @@ func runFetch(_ *cobra.Command, _ []string) {
log.Fatalf("could not get working directory: %v", err)
}

err = srpmproc.Fetch(os.Stdout, cdnUrl, wd)
err = srpmproc.Fetch(os.Stdout, cdnUrl, wd, nil)
if err != nil {
log.Fatal(err)
}
}

func init() {
root.AddCommand(fetch)
}
47 changes: 30 additions & 17 deletions pkg/srpmproc/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package srpmproc
import (
"errors"
"fmt"
"github.com/rocky-linux/srpmproc/pkg/blob"
"github.com/rocky-linux/srpmproc/pkg/data"
"io"
"io/ioutil"
Expand All @@ -13,7 +14,7 @@ import (
"strings"
)

func Fetch(logger io.Writer, cdnUrl string, dir string) error {
func Fetch(logger io.Writer, cdnUrl string, dir string, storage blob.Storage) error {
pd := &data.ProcessData{
Log: log.New(logger, "", log.LstdFlags),
}
Expand Down Expand Up @@ -58,26 +59,38 @@ func Fetch(logger io.Writer, cdnUrl string, dir string) error {
path := lineInfo[1]

url := fmt.Sprintf("%s/%s", cdnUrl, hash)
if storage != nil {
url = hash
}
pd.Log.Printf("downloading %s", url)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("could not create new http request: %v", err)
}
req.Header.Set("Accept-Encoding", "*")
var body []byte

resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("could not download dist-git file: %v", err)
}
if storage != nil {
body, err = storage.Read(hash)
if err != nil {
return fmt.Errorf("could not read blob: %v", err)
}
} else {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("could not create new http request: %v", err)
}
req.Header.Set("Accept-Encoding", "*")

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("could not read the whole dist-git file: %v", err)
}
err = resp.Body.Close()
if err != nil {
return fmt.Errorf("could not close body handle: %v", err)
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("could not download dist-git file: %v", err)
}

body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("could not read the whole dist-git file: %v", err)
}
err = resp.Body.Close()
if err != nil {
return fmt.Errorf("could not close body handle: %v", err)
}
}

hasher := pd.CompareHash(body, hash)
Expand Down

0 comments on commit 8f55eb3

Please sign in to comment.