Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support file:// scheme for repo URLs #63

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions pkg/repo/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -186,7 +187,7 @@ func (r *RepoFetcherImpl) fetchFile(fileType string, repo *bazeldnf.Repository,
log.Infof("Loading %s file from %s", fileType, fileURL)
resp, err := r.Getter.Get(fileURL)
if err != nil {
return fmt.Errorf("Failed to load promary repository file from %s: %v", fileURL, err)
return fmt.Errorf("Failed to load primary repository file from %s: %v", fileURL, err)
}
sha := sha256.New()
defer resp.Body.Close()
Expand Down Expand Up @@ -214,8 +215,29 @@ type Getter interface {

type getterImpl struct{}

func (*getterImpl) Get(url string) (resp *http.Response, err error) {
return http.Get(url)
func fileGet(filename string) (*http.Response, error) {
fp, err := os.Open(filename)
if err != nil {
return nil, err // skipped wrapping the error since the error already begins with "open: "
}

resp := &http.Response{
Status: "OK",
StatusCode: http.StatusOK,
Body: fp,
}
return resp, nil
}

func (*getterImpl) Get(rawURL string) (*http.Response, error) {
u, err := url.Parse(rawURL)
if err != nil {
return nil, fmt.Errorf("Failed to parse URL: %w", err)
}
if u.Scheme == "file" {
return fileGet(u.Path)
}
return http.Get(rawURL)
}

func toHex(hasher hash.Hash) string {
Expand Down
Loading