From 4175b5dba0f605dc4d8cb48c7c6614082382bf85 Mon Sep 17 00:00:00 2001 From: eyalbe4 Date: Wed, 7 Feb 2018 15:59:31 +0200 Subject: [PATCH] From args to flags, fetch command fix and backward compatibility fix. --- cmd/helm/fetch.go | 4 +++- cmd/helm/install.go | 2 +- cmd/helm/repo_add.go | 21 ++++----------------- docs/chart_repository.md | 4 ++-- pkg/repo/chartrepo.go | 9 ++++++++- pkg/repo/chartrepo_test.go | 12 ++++++------ 6 files changed, 24 insertions(+), 28 deletions(-) diff --git a/cmd/helm/fetch.go b/cmd/helm/fetch.go index 612ad973a..8ccfd8793 100644 --- a/cmd/helm/fetch.go +++ b/cmd/helm/fetch.go @@ -96,6 +96,8 @@ func newFetchCmd(out io.Writer) *cobra.Command { } f := cmd.Flags() + f.StringVar(&fch.username, "username", "", "chart repository username") + f.StringVar(&fch.password, "password", "", "chart repository password") f.BoolVar(&fch.untar, "untar", false, "if set to true, will untar the chart after downloading it") f.StringVar(&fch.untardir, "untardir", ".", "if untar is specified, this flag specifies the name of the directory into which the chart is expanded") f.BoolVar(&fch.verify, "verify", false, "verify the package against its signature") @@ -142,7 +144,7 @@ func (f *fetchCmd) run() error { } if f.repoURL != "" { - chartURL, err := repo.FindChartInRepoURL(f.repoURL, f.username, f.password, f.chartRef, f.version, f.certFile, f.keyFile, f.caFile, getter.All(settings)) + chartURL, err := repo.FindChartInAuthRepoURL(f.repoURL, f.username, f.password, f.chartRef, f.version, f.certFile, f.keyFile, f.caFile, getter.All(settings)) if err != nil { return err } diff --git a/cmd/helm/install.go b/cmd/helm/install.go index f3debd913..a02f543db 100644 --- a/cmd/helm/install.go +++ b/cmd/helm/install.go @@ -425,7 +425,7 @@ func locateChartPath(repoURL, username, password, name, version string, verify b dl.Verify = downloader.VerifyAlways } if repoURL != "" { - chartURL, err := repo.FindChartInRepoURL(repoURL, username, password, name, version, + chartURL, err := repo.FindChartInAuthRepoURL(repoURL, username, password, name, version, certFile, keyFile, caFile, getter.All(settings)) if err != nil { return "", err diff --git a/cmd/helm/repo_add.go b/cmd/helm/repo_add.go index bbf0de9f8..77a64cc89 100644 --- a/cmd/helm/repo_add.go +++ b/cmd/helm/repo_add.go @@ -46,30 +46,15 @@ func newRepoAddCmd(out io.Writer) *cobra.Command { add := &repoAddCmd{out: out} cmd := &cobra.Command{ - Use: "add [flags] [NAME] [URL] [USERNAME] [PASSWORD]", + Use: "add [flags] [NAME] [URL]", Short: "add a chart repository", RunE: func(cmd *cobra.Command, args []string) error { - argsDesc := []string{ - "name for the chart repository", - "the url of the chart repository", - "the username for the chart repository", - "the password of the chart repository", - } - - if len(args) <= 2 { - if err := checkArgsLength(len(args), argsDesc[0], argsDesc[1]); err != nil { - return err - } - } else if err := checkArgsLength(len(args), argsDesc[0], argsDesc[1], argsDesc[2], argsDesc[3]); err != nil { + if err := checkArgsLength(len(args), "name for the chart repository", "the url of the chart repository"); err != nil { return err } add.name = args[0] add.url = args[1] - if len(args) == 4 { - add.username = args[2] - add.password = args[3] - } add.home = settings.Home return add.run() @@ -77,6 +62,8 @@ func newRepoAddCmd(out io.Writer) *cobra.Command { } f := cmd.Flags() + f.StringVar(&add.username, "username", "", "chart repository username") + f.StringVar(&add.password, "password", "", "chart repository password") f.BoolVar(&add.noupdate, "no-update", false, "raise error if repo is already registered") f.StringVar(&add.certFile, "cert-file", "", "identify HTTPS client using this SSL certificate file") f.StringVar(&add.keyFile, "key-file", "", "identify HTTPS client using this SSL key file") diff --git a/docs/chart_repository.md b/docs/chart_repository.md index 21043dd12..9d3837bed 100644 --- a/docs/chart_repository.md +++ b/docs/chart_repository.md @@ -275,8 +275,8 @@ fantastic-charts https://fantastic-charts.storage.googleapis.com If the charts are backed by HTTP basic authentication, you can also supply the username and password here: -```console -$ helm repo add fantastic-charts https://username:password@fantastic-charts.storage.googleapis.com my-username my-password +``console +$ helm repo add fantastic-charts https://fantastic-charts.storage.googleapis.com --username my-username--password my-password $ helm repo list fantastic-charts https://username:password@fantastic-charts.storage.googleapis.com ``` diff --git a/pkg/repo/chartrepo.go b/pkg/repo/chartrepo.go index 0c004d1bc..b73dda52f 100644 --- a/pkg/repo/chartrepo.go +++ b/pkg/repo/chartrepo.go @@ -188,7 +188,14 @@ func (r *ChartRepository) generateIndex() error { // FindChartInRepoURL finds chart in chart repository pointed by repoURL // without adding repo to repositories -func FindChartInRepoURL(repoURL, username, password, chartName, chartVersion, certFile, keyFile, caFile string, getters getter.Providers) (string, error) { +func FindChartInRepoURL(repoURL, chartName, chartVersion, certFile, keyFile, caFile string, getters getter.Providers) (string, error) { + return FindChartInAuthRepoURL(repoURL, "", "", chartName, chartVersion, certFile, keyFile, caFile, getters) +} + +// FindChartInRepoURL finds chart in chart repository pointed by repoURL +// without adding repo to repositories. +// Unlike the FindChartInRepoURL function, this function also receives credentials for the chart repository. +func FindChartInAuthRepoURL(repoURL, username, password, chartName, chartVersion, certFile, keyFile, caFile string, getters getter.Providers) (string, error) { // Download and write the index file to a temporary location tempIndexFile, err := ioutil.TempFile("", "tmp-repo-file") diff --git a/pkg/repo/chartrepo_test.go b/pkg/repo/chartrepo_test.go index fc12fb009..948ee12d3 100644 --- a/pkg/repo/chartrepo_test.go +++ b/pkg/repo/chartrepo_test.go @@ -221,7 +221,7 @@ func TestFindChartInRepoURL(t *testing.T) { } defer srv.Close() - chartURL, err := FindChartInRepoURL(srv.URL, "", "", "nginx", "", "", "", "", getter.All(environment.EnvSettings{})) + chartURL, err := FindChartInRepoURL(srv.URL, "nginx", "", "", "", "", getter.All(environment.EnvSettings{})) if err != nil { t.Errorf("%s", err) } @@ -229,7 +229,7 @@ func TestFindChartInRepoURL(t *testing.T) { t.Errorf("%s is not the valid URL", chartURL) } - chartURL, err = FindChartInRepoURL(srv.URL, "", "", "nginx", "0.1.0", "", "", "", getter.All(environment.EnvSettings{})) + chartURL, err = FindChartInRepoURL(srv.URL, "nginx", "0.1.0", "", "", "", getter.All(environment.EnvSettings{})) if err != nil { t.Errorf("%s", err) } @@ -239,7 +239,7 @@ func TestFindChartInRepoURL(t *testing.T) { } func TestErrorFindChartInRepoURL(t *testing.T) { - _, err := FindChartInRepoURL("http://someserver/something", "", "", "nginx", "", "", "", "", getter.All(environment.EnvSettings{})) + _, err := FindChartInRepoURL("http://someserver/something", "nginx", "", "", "", "", getter.All(environment.EnvSettings{})) if err == nil { t.Errorf("Expected error for bad chart URL, but did not get any errors") } @@ -253,7 +253,7 @@ func TestErrorFindChartInRepoURL(t *testing.T) { } defer srv.Close() - _, err = FindChartInRepoURL(srv.URL, "", "", "nginx1", "", "", "", "", getter.All(environment.EnvSettings{})) + _, err = FindChartInRepoURL(srv.URL, "nginx1", "", "", "", "", getter.All(environment.EnvSettings{})) if err == nil { t.Errorf("Expected error for chart not found, but did not get any errors") } @@ -261,7 +261,7 @@ func TestErrorFindChartInRepoURL(t *testing.T) { t.Errorf("Expected error for chart not found, but got a different error (%v)", err) } - _, err = FindChartInRepoURL(srv.URL, "", "", "nginx1", "0.1.0", "", "", "", getter.All(environment.EnvSettings{})) + _, err = FindChartInRepoURL(srv.URL, "nginx1", "0.1.0", "", "", "", getter.All(environment.EnvSettings{})) if err == nil { t.Errorf("Expected error for chart not found, but did not get any errors") } @@ -269,7 +269,7 @@ func TestErrorFindChartInRepoURL(t *testing.T) { t.Errorf("Expected error for chart not found, but got a different error (%v)", err) } - _, err = FindChartInRepoURL(srv.URL, "", "", "chartWithNoURL", "", "", "", "", getter.All(environment.EnvSettings{})) + _, err = FindChartInRepoURL(srv.URL, "chartWithNoURL", "", "", "", "", getter.All(environment.EnvSettings{})) if err == nil { t.Errorf("Expected error for no chart URLs available, but did not get any errors") }