Skip to content

Commit

Permalink
feat(index/fetch) Add support to guess index backend based on URI scheme
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Frank <[email protected]>
  • Loading branch information
max-frank committed Jul 5, 2023
1 parent 747ca23 commit 856b156
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions pkg/index/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package index
import (
"context"
"fmt"
"net/url"
"strings"

"gopkg.in/yaml.v3"
Expand All @@ -30,17 +31,22 @@ type FetchFunc func(context.Context, *config.Entry) ([]byte, error)

// Fetcher can fetch indices from various storage backends.
type Fetcher struct {
fetchFuncs map[string]FetchFunc
fetchFuncs map[string]FetchFunc
schemeDefaultBackends map[string]string
}

// NewFetcher creates a new index fetcher.
func NewFetcher() *Fetcher {
return &Fetcher{
fetchFuncs: map[string]FetchFunc{
"": http.Fetch,
// for convenient UX we map the HTTP backend to both HTTP and HTTPS
"http": http.Fetch,
"https": http.Fetch,
},
schemeDefaultBackends: map[string]string{
"http": "http",
"https": "https",
},
}
}

Expand All @@ -54,6 +60,21 @@ func (f *Fetcher) get(backend string) (FetchFunc, error) {

// Fetch retrieves a remote index.
func (f *Fetcher) Fetch(ctx context.Context, conf *config.Entry) (*Index, error) {
// if we don't have an explicit backend
// we try to guess based on the URI scheme
if conf.Backend == "" {
url, err := url.Parse(conf.URL)

Check failure on line 66 in pkg/index/fetch.go

View workflow job for this annotation

GitHub Actions / Lint golang files

importShadow: shadow of imported package 'url' (gocritic)
if err != nil {
return nil, fmt.Errorf("unable to parse index url: %w", err)
}
if mappedBackend, ok := f.schemeDefaultBackends[strings.ToLower(url.Scheme)]; ok {
conf.Backend = mappedBackend
} else {
// if we cant guess we try the default HTTP
conf.Backend = "http"
}
}

fetcher, err := f.get(conf.Backend)
if err != nil {
return nil, err
Expand Down

0 comments on commit 856b156

Please sign in to comment.