From 685e730ba8faf88d444131506a90943fad54e264 Mon Sep 17 00:00:00 2001 From: Anton Galitsyn Date: Mon, 9 Jan 2017 17:53:33 +0700 Subject: [PATCH] create repo.Getter interface --- cmd/helm/downloader/chart_downloader.go | 17 ++++++++--------- pkg/repo/chartrepo.go | 14 +++++++++++++- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/cmd/helm/downloader/chart_downloader.go b/cmd/helm/downloader/chart_downloader.go index 57e79e9c8..610eeca22 100644 --- a/cmd/helm/downloader/chart_downloader.go +++ b/cmd/helm/downloader/chart_downloader.go @@ -21,7 +21,6 @@ import ( "fmt" "io" "io/ioutil" - "net/http" "net/url" "os" "path/filepath" @@ -76,12 +75,12 @@ type ChartDownloader struct { // 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. 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 { return "", nil, err } - data, err := download(u.String(), client) + data, err := download(u.String(), r) if err != nil { return "", nil, err } @@ -95,7 +94,7 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven // If provenance is requested, verify it. ver := &provenance.Verification{} if c.Verify > VerifyNever { - body, err := download(u.String()+".prov", client) + body, err := download(u.String()+".prov", r) if err != nil { if c.Verify == VerifyAlways { 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 // * 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 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) if err != nil { 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 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. @@ -228,11 +227,11 @@ func VerifyChart(path string, keyring string) (*provenance.Verification, error) return sig.Verify(path, provfile) } -// download performs a HTTP Get using specified client and returns the body. -func download(href string, client *http.Client) (*bytes.Buffer, error) { +// download performs a Get from repo.Getter and returns the body. +func download(href string, r repo.Getter) (*bytes.Buffer, error) { buf := bytes.NewBuffer(nil) - resp, err := client.Get(href) + resp, err := r.Get(href) if err != nil { return buf, err } diff --git a/pkg/repo/chartrepo.go b/pkg/repo/chartrepo.go index 523e5e1e4..a2991ce79 100644 --- a/pkg/repo/chartrepo.go +++ b/pkg/repo/chartrepo.go @@ -50,6 +50,10 @@ type ChartRepository struct { Client *http.Client } +type Getter interface { + Get(url string) (*http.Response, error) +} + // NewChartRepository constructs ChartRepository func NewChartRepository(cfg *ChartRepositoryConfig) (*ChartRepository, error) { var client *http.Client @@ -82,6 +86,14 @@ func NewChartRepository(cfg *ChartRepositoryConfig) (*ChartRepository, error) { }, 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. // // It requires the presence of an index.yaml file in the directory. @@ -119,7 +131,7 @@ func (r *ChartRepository) DownloadIndexFile() error { var indexURL string indexURL = strings.TrimSuffix(r.Config.URL, "/") + "/index.yaml" - resp, err := r.Client.Get(indexURL) + resp, err := r.Get(indexURL) if err != nil { return err }