Commit 685e730b authored by Anton Galitsyn's avatar Anton Galitsyn

create repo.Getter interface

parent 8a1d43ec
...@@ -21,7 +21,6 @@ import ( ...@@ -21,7 +21,6 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
...@@ -76,12 +75,12 @@ type ChartDownloader struct { ...@@ -76,12 +75,12 @@ type ChartDownloader struct {
// Returns a string path to the location where the file was downloaded and a verification // Returns a string path to the location where the file was downloaded and a verification
// (if provenance was verified), or an error if something bad happened. // (if provenance was verified), or an error if something bad happened.
func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *provenance.Verification, error) { func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *provenance.Verification, error) {
u, client, err := c.ResolveChartVersion(ref, version) u, r, err := c.ResolveChartVersion(ref, version)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
data, err := download(u.String(), client) data, err := download(u.String(), r)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
...@@ -95,7 +94,7 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven ...@@ -95,7 +94,7 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven
// If provenance is requested, verify it. // If provenance is requested, verify it.
ver := &provenance.Verification{} ver := &provenance.Verification{}
if c.Verify > VerifyNever { if c.Verify > VerifyNever {
body, err := download(u.String()+".prov", client) body, err := download(u.String()+".prov", r)
if err != nil { if err != nil {
if c.Verify == VerifyAlways { if c.Verify == VerifyAlways {
return destfile, ver, fmt.Errorf("Failed to fetch provenance %q", u.String()+".prov") return destfile, ver, fmt.Errorf("Failed to fetch provenance %q", u.String()+".prov")
...@@ -131,7 +130,7 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven ...@@ -131,7 +130,7 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven
// * If version is non-empty, this will return the URL for that version // * If version is non-empty, this will return the URL for that version
// * If version is empty, this will return the URL for the latest version // * If version is empty, this will return the URL for the latest version
// * If no version can be found, an error is returned // * If no version can be found, an error is returned
func (c *ChartDownloader) ResolveChartVersion(ref, version string) (*url.URL, *http.Client, error) { func (c *ChartDownloader) ResolveChartVersion(ref, version string) (*url.URL, *repo.ChartRepository, error) {
u, err := url.Parse(ref) u, err := url.Parse(ref)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("invalid chart URL format: %s", ref) return nil, nil, fmt.Errorf("invalid chart URL format: %s", ref)
...@@ -199,7 +198,7 @@ func (c *ChartDownloader) ResolveChartVersion(ref, version string) (*url.URL, *h ...@@ -199,7 +198,7 @@ func (c *ChartDownloader) ResolveChartVersion(ref, version string) (*url.URL, *h
return nil, nil, fmt.Errorf("invalid chart URL format: %s", ref) return nil, nil, fmt.Errorf("invalid chart URL format: %s", ref)
} }
return u, r.Client, nil return u, r, nil
} }
// VerifyChart takes a path to a chart archive and a keyring, and verifies the chart. // VerifyChart takes a path to a chart archive and a keyring, and verifies the chart.
...@@ -228,11 +227,11 @@ func VerifyChart(path string, keyring string) (*provenance.Verification, error) ...@@ -228,11 +227,11 @@ func VerifyChart(path string, keyring string) (*provenance.Verification, error)
return sig.Verify(path, provfile) return sig.Verify(path, provfile)
} }
// download performs a HTTP Get using specified client and returns the body. // download performs a Get from repo.Getter and returns the body.
func download(href string, client *http.Client) (*bytes.Buffer, error) { func download(href string, r repo.Getter) (*bytes.Buffer, error) {
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
resp, err := client.Get(href) resp, err := r.Get(href)
if err != nil { if err != nil {
return buf, err return buf, err
} }
......
...@@ -50,6 +50,10 @@ type ChartRepository struct { ...@@ -50,6 +50,10 @@ type ChartRepository struct {
Client *http.Client Client *http.Client
} }
type Getter interface {
Get(url string) (*http.Response, error)
}
// NewChartRepository constructs ChartRepository // NewChartRepository constructs ChartRepository
func NewChartRepository(cfg *ChartRepositoryConfig) (*ChartRepository, error) { func NewChartRepository(cfg *ChartRepositoryConfig) (*ChartRepository, error) {
var client *http.Client var client *http.Client
...@@ -82,6 +86,14 @@ func NewChartRepository(cfg *ChartRepositoryConfig) (*ChartRepository, error) { ...@@ -82,6 +86,14 @@ func NewChartRepository(cfg *ChartRepositoryConfig) (*ChartRepository, error) {
}, nil }, nil
} }
func (r *ChartRepository) Get(url string) (*http.Response, error) {
resp, err := r.Client.Get(url)
if err != nil {
return nil, err
}
return resp, nil
}
// Load loads a directory of charts as if it were a repository. // Load loads a directory of charts as if it were a repository.
// //
// It requires the presence of an index.yaml file in the directory. // It requires the presence of an index.yaml file in the directory.
...@@ -119,7 +131,7 @@ func (r *ChartRepository) DownloadIndexFile() error { ...@@ -119,7 +131,7 @@ func (r *ChartRepository) DownloadIndexFile() error {
var indexURL string var indexURL string
indexURL = strings.TrimSuffix(r.Config.URL, "/") + "/index.yaml" indexURL = strings.TrimSuffix(r.Config.URL, "/") + "/index.yaml"
resp, err := r.Client.Get(indexURL) resp, err := r.Get(indexURL)
if err != nil { if err != nil {
return err return err
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment