Skip to content

Commit e65d2a3

Browse files
authored
Merge pull request #63 from wwade/main
Support file:// scheme for repo URLs
2 parents 26b99c1 + 1120239 commit e65d2a3

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

pkg/repo/fetch.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"net/http"
1010
"net/url"
11+
"os"
1112
"path"
1213
"path/filepath"
1314
"strings"
@@ -186,7 +187,7 @@ func (r *RepoFetcherImpl) fetchFile(fileType string, repo *bazeldnf.Repository,
186187
log.Infof("Loading %s file from %s", fileType, fileURL)
187188
resp, err := r.Getter.Get(fileURL)
188189
if err != nil {
189-
return fmt.Errorf("Failed to load promary repository file from %s: %v", fileURL, err)
190+
return fmt.Errorf("Failed to load primary repository file from %s: %v", fileURL, err)
190191
}
191192
sha := sha256.New()
192193
defer resp.Body.Close()
@@ -214,8 +215,29 @@ type Getter interface {
214215

215216
type getterImpl struct{}
216217

217-
func (*getterImpl) Get(url string) (resp *http.Response, err error) {
218-
return http.Get(url)
218+
func fileGet(filename string) (*http.Response, error) {
219+
fp, err := os.Open(filename)
220+
if err != nil {
221+
return nil, err // skipped wrapping the error since the error already begins with "open: "
222+
}
223+
224+
resp := &http.Response{
225+
Status: "OK",
226+
StatusCode: http.StatusOK,
227+
Body: fp,
228+
}
229+
return resp, nil
230+
}
231+
232+
func (*getterImpl) Get(rawURL string) (*http.Response, error) {
233+
u, err := url.Parse(rawURL)
234+
if err != nil {
235+
return nil, fmt.Errorf("Failed to parse URL: %w", err)
236+
}
237+
if u.Scheme == "file" {
238+
return fileGet(u.Path)
239+
}
240+
return http.Get(rawURL)
219241
}
220242

221243
func toHex(hasher hash.Hash) string {

0 commit comments

Comments
 (0)