From 1b251fbe0bb69e78e700d2d0d350b0eb13459aef Mon Sep 17 00:00:00 2001 From: Dmitriy Ermakov Date: Sat, 12 Dec 2020 11:05:50 +0300 Subject: [PATCH 1/5] Added flag --list-repo The flag makes helm print the chart repo URL to use with helm repo add. Issue: #7419 Signed-off-by: Dmitriy Ermakov --- cmd/helm/search_hub.go | 41 +++++++++++++++++++++++++++---------- cmd/helm/search_hub_test.go | 28 +++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/cmd/helm/search_hub.go b/cmd/helm/search_hub.go index 89139ec16..4cbf64991 100644 --- a/cmd/helm/search_hub.go +++ b/cmd/helm/search_hub.go @@ -45,6 +45,7 @@ type searchHubOptions struct { searchEndpoint string maxColWidth uint outputFormat output.Format + listRepo bool } func newSearchHubCmd(out io.Writer) *cobra.Command { @@ -62,6 +63,7 @@ func newSearchHubCmd(out io.Writer) *cobra.Command { f := cmd.Flags() f.StringVar(&o.searchEndpoint, "endpoint", "https://hub.helm.sh", "monocular instance to query for charts") f.UintVar(&o.maxColWidth, "max-col-width", 50, "maximum column width for output table") + f.BoolVar(&o.listRepo, "list-repo", false, "toggle chart repo URL display") bindOutputFlag(cmd, &o.outputFormat) return cmd @@ -80,28 +82,35 @@ func (o *searchHubOptions) run(out io.Writer, args []string) error { return fmt.Errorf("unable to perform search against %q", o.searchEndpoint) } - return o.outputFormat.Write(out, newHubSearchWriter(results, o.searchEndpoint, o.maxColWidth)) + return o.outputFormat.Write(out, newHubSearchWriter(results, o.searchEndpoint, o.maxColWidth, o.listRepo)) +} + +type hubChartRepo struct { + URL string `json:"url"` + Name string `json:"name"` } type hubChartElement struct { - URL string `json:"url"` - Version string `json:"version"` - AppVersion string `json:"app_version"` - Description string `json:"description"` + URL string `json:"url"` + Version string `json:"version"` + AppVersion string `json:"app_version"` + Description string `json:"description"` + Repository hubChartRepo `json:"repository"` } type hubSearchWriter struct { elements []hubChartElement columnWidth uint + listRepo bool } -func newHubSearchWriter(results []monocular.SearchResult, endpoint string, columnWidth uint) *hubSearchWriter { +func newHubSearchWriter(results []monocular.SearchResult, endpoint string, columnWidth uint, listRepo bool) *hubSearchWriter { var elements []hubChartElement for _, r := range results { url := endpoint + "/charts/" + r.ID - elements = append(elements, hubChartElement{url, r.Relationships.LatestChartVersion.Data.Version, r.Relationships.LatestChartVersion.Data.AppVersion, r.Attributes.Description}) + elements = append(elements, hubChartElement{url, r.Relationships.LatestChartVersion.Data.Version, r.Relationships.LatestChartVersion.Data.AppVersion, r.Attributes.Description, hubChartRepo{URL: r.Attributes.Repo.URL, Name: r.Attributes.Repo.Name}}) } - return &hubSearchWriter{elements, columnWidth} + return &hubSearchWriter{elements, columnWidth, listRepo} } func (h *hubSearchWriter) WriteTable(out io.Writer) error { @@ -114,9 +123,19 @@ func (h *hubSearchWriter) WriteTable(out io.Writer) error { } table := uitable.New() table.MaxColWidth = h.columnWidth - table.AddRow("URL", "CHART VERSION", "APP VERSION", "DESCRIPTION") + + if h.listRepo { + table.AddRow("REPO", "URL", "CHART VERSION", "APP VERSION", "DESCRIPTION") + } else { + table.AddRow("URL", "CHART VERSION", "APP VERSION", "DESCRIPTION") + } + for _, r := range h.elements { - table.AddRow(r.URL, r.Version, r.AppVersion, r.Description) + if h.listRepo { + table.AddRow(r.Repository.URL, r.URL, r.Version, r.AppVersion, r.Description) + } else { + table.AddRow(r.URL, r.Version, r.AppVersion, r.Description) + } } return output.EncodeTable(out, table) } @@ -134,7 +153,7 @@ func (h *hubSearchWriter) encodeByFormat(out io.Writer, format output.Format) er chartList := make([]hubChartElement, 0, len(h.elements)) for _, r := range h.elements { - chartList = append(chartList, hubChartElement{r.URL, r.Version, r.AppVersion, r.Description}) + chartList = append(chartList, hubChartElement{r.URL, r.Version, r.AppVersion, r.Description, r.Repository}) } switch format { diff --git a/cmd/helm/search_hub_test.go b/cmd/helm/search_hub_test.go index 9fc21cab8..9b56936c7 100644 --- a/cmd/helm/search_hub_test.go +++ b/cmd/helm/search_hub_test.go @@ -51,6 +51,34 @@ func TestSearchHubCmd(t *testing.T) { } } +func TestSearchHubListRepoCmd(t *testing.T) { + + // Setup a mock search service + var searchResult = `{"data":[{"id":"stable/phpmyadmin","type":"chart","attributes":{"name":"phpmyadmin","repo":{"name":"stable","url":"https://charts.helm.sh/stable"},"description":"phpMyAdmin is an mysql administration frontend","home":"https://www.phpmyadmin.net/","keywords":["mariadb","mysql","phpmyadmin"],"maintainers":[{"name":"Bitnami","email":"containers@bitnami.com"}],"sources":["https://github.com/bitnami/bitnami-docker-phpmyadmin"],"icon":""},"links":{"self":"/v1/charts/stable/phpmyadmin"},"relationships":{"latestChartVersion":{"data":{"version":"3.0.0","app_version":"4.9.0-1","created":"2019-08-08T17:57:31.38Z","digest":"119c499251bffd4b06ff0cd5ac98c2ce32231f84899fb4825be6c2d90971c742","urls":["https://charts.helm.sh/stable/phpmyadmin-3.0.0.tgz"],"readme":"/v1/assets/stable/phpmyadmin/versions/3.0.0/README.md","values":"/v1/assets/stable/phpmyadmin/versions/3.0.0/values.yaml"},"links":{"self":"/v1/charts/stable/phpmyadmin/versions/3.0.0"}}}},{"id":"bitnami/phpmyadmin","type":"chart","attributes":{"name":"phpmyadmin","repo":{"name":"bitnami","url":"https://charts.bitnami.com"},"description":"phpMyAdmin is an mysql administration frontend","home":"https://www.phpmyadmin.net/","keywords":["mariadb","mysql","phpmyadmin"],"maintainers":[{"name":"Bitnami","email":"containers@bitnami.com"}],"sources":["https://github.com/bitnami/bitnami-docker-phpmyadmin"],"icon":""},"links":{"self":"/v1/charts/bitnami/phpmyadmin"},"relationships":{"latestChartVersion":{"data":{"version":"3.0.0","app_version":"4.9.0-1","created":"2019-08-08T18:34:13.341Z","digest":"66d77cf6d8c2b52c488d0a294cd4996bd5bad8dc41d3829c394498fb401c008a","urls":["https://charts.bitnami.com/bitnami/phpmyadmin-3.0.0.tgz"],"readme":"/v1/assets/bitnami/phpmyadmin/versions/3.0.0/README.md","values":"/v1/assets/bitnami/phpmyadmin/versions/3.0.0/values.yaml"},"links":{"self":"/v1/charts/bitnami/phpmyadmin/versions/3.0.0"}}}}]}` + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, searchResult) + })) + defer ts.Close() + + // The expected output has the URL to the mocked search service in it + var expected = fmt.Sprintf(`REPO URL CHART VERSION APP VERSION DESCRIPTION +https://charts.helm.sh/stable %s/charts/stable/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend +https://charts.bitnami.com %s/charts/bitnami/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend +`, ts.URL, ts.URL) + + testcmd := "search hub --list-repo --endpoint " + ts.URL + " maria" + storage := storageFixture() + _, out, err := executeActionCommandC(storage, testcmd) + if err != nil { + t.Errorf("unexpected error, %s", err) + } + if out != expected { + t.Error("expected and actual output did not match") + t.Log(out) + t.Log(expected) + } +} + func TestSearchHubOutputCompletion(t *testing.T) { outputFlagCompletionTest(t, "search hub") } From 3d0bb987cb29fcbef5dc8d1ca38097e943a2a039 Mon Sep 17 00:00:00 2001 From: Dmitriy Ermakov Date: Mon, 25 Jan 2021 19:58:09 +0100 Subject: [PATCH 2/5] Removed list-repo flag. Print Repo URL by default Signed-off-by: Dmitriy Ermakov --- cmd/helm/search_hub.go | 22 ++++++---------------- cmd/helm/search_hub_test.go | 34 +++------------------------------- 2 files changed, 9 insertions(+), 47 deletions(-) diff --git a/cmd/helm/search_hub.go b/cmd/helm/search_hub.go index 4cbf64991..a74759a03 100644 --- a/cmd/helm/search_hub.go +++ b/cmd/helm/search_hub.go @@ -45,7 +45,6 @@ type searchHubOptions struct { searchEndpoint string maxColWidth uint outputFormat output.Format - listRepo bool } func newSearchHubCmd(out io.Writer) *cobra.Command { @@ -63,7 +62,7 @@ func newSearchHubCmd(out io.Writer) *cobra.Command { f := cmd.Flags() f.StringVar(&o.searchEndpoint, "endpoint", "https://hub.helm.sh", "monocular instance to query for charts") f.UintVar(&o.maxColWidth, "max-col-width", 50, "maximum column width for output table") - f.BoolVar(&o.listRepo, "list-repo", false, "toggle chart repo URL display") + bindOutputFlag(cmd, &o.outputFormat) return cmd @@ -82,7 +81,7 @@ func (o *searchHubOptions) run(out io.Writer, args []string) error { return fmt.Errorf("unable to perform search against %q", o.searchEndpoint) } - return o.outputFormat.Write(out, newHubSearchWriter(results, o.searchEndpoint, o.maxColWidth, o.listRepo)) + return o.outputFormat.Write(out, newHubSearchWriter(results, o.searchEndpoint, o.maxColWidth)) } type hubChartRepo struct { @@ -101,16 +100,15 @@ type hubChartElement struct { type hubSearchWriter struct { elements []hubChartElement columnWidth uint - listRepo bool } -func newHubSearchWriter(results []monocular.SearchResult, endpoint string, columnWidth uint, listRepo bool) *hubSearchWriter { +func newHubSearchWriter(results []monocular.SearchResult, endpoint string, columnWidth uint) *hubSearchWriter { var elements []hubChartElement for _, r := range results { url := endpoint + "/charts/" + r.ID elements = append(elements, hubChartElement{url, r.Relationships.LatestChartVersion.Data.Version, r.Relationships.LatestChartVersion.Data.AppVersion, r.Attributes.Description, hubChartRepo{URL: r.Attributes.Repo.URL, Name: r.Attributes.Repo.Name}}) } - return &hubSearchWriter{elements, columnWidth, listRepo} + return &hubSearchWriter{elements, columnWidth} } func (h *hubSearchWriter) WriteTable(out io.Writer) error { @@ -124,18 +122,10 @@ func (h *hubSearchWriter) WriteTable(out io.Writer) error { table := uitable.New() table.MaxColWidth = h.columnWidth - if h.listRepo { - table.AddRow("REPO", "URL", "CHART VERSION", "APP VERSION", "DESCRIPTION") - } else { - table.AddRow("URL", "CHART VERSION", "APP VERSION", "DESCRIPTION") - } + table.AddRow("URL", "CHART VERSION", "APP VERSION", "DESCRIPTION", "REPO") for _, r := range h.elements { - if h.listRepo { - table.AddRow(r.Repository.URL, r.URL, r.Version, r.AppVersion, r.Description) - } else { - table.AddRow(r.URL, r.Version, r.AppVersion, r.Description) - } + table.AddRow(r.URL, r.Version, r.AppVersion, r.Description, r.Repository.URL) } return output.EncodeTable(out, table) } diff --git a/cmd/helm/search_hub_test.go b/cmd/helm/search_hub_test.go index 9b56936c7..564c46775 100644 --- a/cmd/helm/search_hub_test.go +++ b/cmd/helm/search_hub_test.go @@ -33,9 +33,9 @@ func TestSearchHubCmd(t *testing.T) { defer ts.Close() // The expected output has the URL to the mocked search service in it - var expected = fmt.Sprintf(`URL CHART VERSION APP VERSION DESCRIPTION -%s/charts/stable/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend -%s/charts/bitnami/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend + var expected = fmt.Sprintf(`URL CHART VERSION APP VERSION DESCRIPTION REPO +%s/charts/stable/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend https://charts.helm.sh/stable +%s/charts/bitnami/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend https://charts.bitnami.com `, ts.URL, ts.URL) testcmd := "search hub --endpoint " + ts.URL + " maria" @@ -51,34 +51,6 @@ func TestSearchHubCmd(t *testing.T) { } } -func TestSearchHubListRepoCmd(t *testing.T) { - - // Setup a mock search service - var searchResult = `{"data":[{"id":"stable/phpmyadmin","type":"chart","attributes":{"name":"phpmyadmin","repo":{"name":"stable","url":"https://charts.helm.sh/stable"},"description":"phpMyAdmin is an mysql administration frontend","home":"https://www.phpmyadmin.net/","keywords":["mariadb","mysql","phpmyadmin"],"maintainers":[{"name":"Bitnami","email":"containers@bitnami.com"}],"sources":["https://github.com/bitnami/bitnami-docker-phpmyadmin"],"icon":""},"links":{"self":"/v1/charts/stable/phpmyadmin"},"relationships":{"latestChartVersion":{"data":{"version":"3.0.0","app_version":"4.9.0-1","created":"2019-08-08T17:57:31.38Z","digest":"119c499251bffd4b06ff0cd5ac98c2ce32231f84899fb4825be6c2d90971c742","urls":["https://charts.helm.sh/stable/phpmyadmin-3.0.0.tgz"],"readme":"/v1/assets/stable/phpmyadmin/versions/3.0.0/README.md","values":"/v1/assets/stable/phpmyadmin/versions/3.0.0/values.yaml"},"links":{"self":"/v1/charts/stable/phpmyadmin/versions/3.0.0"}}}},{"id":"bitnami/phpmyadmin","type":"chart","attributes":{"name":"phpmyadmin","repo":{"name":"bitnami","url":"https://charts.bitnami.com"},"description":"phpMyAdmin is an mysql administration frontend","home":"https://www.phpmyadmin.net/","keywords":["mariadb","mysql","phpmyadmin"],"maintainers":[{"name":"Bitnami","email":"containers@bitnami.com"}],"sources":["https://github.com/bitnami/bitnami-docker-phpmyadmin"],"icon":""},"links":{"self":"/v1/charts/bitnami/phpmyadmin"},"relationships":{"latestChartVersion":{"data":{"version":"3.0.0","app_version":"4.9.0-1","created":"2019-08-08T18:34:13.341Z","digest":"66d77cf6d8c2b52c488d0a294cd4996bd5bad8dc41d3829c394498fb401c008a","urls":["https://charts.bitnami.com/bitnami/phpmyadmin-3.0.0.tgz"],"readme":"/v1/assets/bitnami/phpmyadmin/versions/3.0.0/README.md","values":"/v1/assets/bitnami/phpmyadmin/versions/3.0.0/values.yaml"},"links":{"self":"/v1/charts/bitnami/phpmyadmin/versions/3.0.0"}}}}]}` - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintln(w, searchResult) - })) - defer ts.Close() - - // The expected output has the URL to the mocked search service in it - var expected = fmt.Sprintf(`REPO URL CHART VERSION APP VERSION DESCRIPTION -https://charts.helm.sh/stable %s/charts/stable/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend -https://charts.bitnami.com %s/charts/bitnami/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend -`, ts.URL, ts.URL) - - testcmd := "search hub --list-repo --endpoint " + ts.URL + " maria" - storage := storageFixture() - _, out, err := executeActionCommandC(storage, testcmd) - if err != nil { - t.Errorf("unexpected error, %s", err) - } - if out != expected { - t.Error("expected and actual output did not match") - t.Log(out) - t.Log(expected) - } -} - func TestSearchHubOutputCompletion(t *testing.T) { outputFlagCompletionTest(t, "search hub") } From 2dd88e846fd0d848b00b2e393916328f8eaeacd2 Mon Sep 17 00:00:00 2001 From: Dmitrii Ermakov Date: Sun, 15 Aug 2021 11:28:17 +0200 Subject: [PATCH 3/5] Reintroduce --list-repo flag in search repo Signed-off-by: Dmitrii Ermakov --- cmd/helm/search_hub.go | 21 ++++++++++++++++----- cmd/helm/search_hub_test.go | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/cmd/helm/search_hub.go b/cmd/helm/search_hub.go index 002d909ab..ed3f5bdf2 100644 --- a/cmd/helm/search_hub.go +++ b/cmd/helm/search_hub.go @@ -53,6 +53,7 @@ type searchHubOptions struct { searchEndpoint string maxColWidth uint outputFormat output.Format + listRepoURL bool } func newSearchHubCmd(out io.Writer) *cobra.Command { @@ -70,6 +71,7 @@ func newSearchHubCmd(out io.Writer) *cobra.Command { f := cmd.Flags() f.StringVar(&o.searchEndpoint, "endpoint", "https://hub.helm.sh", "Hub instance to query for charts") f.UintVar(&o.maxColWidth, "max-col-width", 50, "maximum column width for output table") + f.BoolVar(&o.listRepoURL, "list-repo", false, "print charts repository URL") bindOutputFlag(cmd, &o.outputFormat) @@ -89,7 +91,7 @@ func (o *searchHubOptions) run(out io.Writer, args []string) error { return fmt.Errorf("unable to perform search against %q", o.searchEndpoint) } - return o.outputFormat.Write(out, newHubSearchWriter(results, o.searchEndpoint, o.maxColWidth)) + return o.outputFormat.Write(out, newHubSearchWriter(results, o.searchEndpoint, o.maxColWidth, o.listRepoURL)) } type hubChartRepo struct { @@ -108,9 +110,10 @@ type hubChartElement struct { type hubSearchWriter struct { elements []hubChartElement columnWidth uint + listRepoURL bool } -func newHubSearchWriter(results []monocular.SearchResult, endpoint string, columnWidth uint) *hubSearchWriter { +func newHubSearchWriter(results []monocular.SearchResult, endpoint string, columnWidth uint, listRepoURL bool) *hubSearchWriter { var elements []hubChartElement for _, r := range results { // Backwards compatibility for Monocular @@ -123,7 +126,7 @@ func newHubSearchWriter(results []monocular.SearchResult, endpoint string, colum elements = append(elements, hubChartElement{url, r.Relationships.LatestChartVersion.Data.Version, r.Relationships.LatestChartVersion.Data.AppVersion, r.Attributes.Description, hubChartRepo{URL: r.Attributes.Repo.URL, Name: r.Attributes.Repo.Name}}) } - return &hubSearchWriter{elements, columnWidth} + return &hubSearchWriter{elements, columnWidth, listRepoURL} } func (h *hubSearchWriter) WriteTable(out io.Writer) error { @@ -137,10 +140,18 @@ func (h *hubSearchWriter) WriteTable(out io.Writer) error { table := uitable.New() table.MaxColWidth = h.columnWidth - table.AddRow("URL", "CHART VERSION", "APP VERSION", "DESCRIPTION", "REPO") + if h.listRepoURL { + table.AddRow("URL", "CHART VERSION", "APP VERSION", "DESCRIPTION", "REPO URL") + } else { + table.AddRow("URL", "CHART VERSION", "APP VERSION", "DESCRIPTION") + } for _, r := range h.elements { - table.AddRow(r.URL, r.Version, r.AppVersion, r.Description, r.Repository.URL) + if h.listRepoURL { + table.AddRow(r.URL, r.Version, r.AppVersion, r.Description, r.Repository.URL) + } else { + table.AddRow(r.URL, r.Version, r.AppVersion, r.Description) + } } return output.EncodeTable(out, table) } diff --git a/cmd/helm/search_hub_test.go b/cmd/helm/search_hub_test.go index 564c46775..3498845fa 100644 --- a/cmd/helm/search_hub_test.go +++ b/cmd/helm/search_hub_test.go @@ -33,12 +33,44 @@ func TestSearchHubCmd(t *testing.T) { defer ts.Close() // The expected output has the URL to the mocked search service in it - var expected = fmt.Sprintf(`URL CHART VERSION APP VERSION DESCRIPTION REPO + // Trailing spaces are necessary to preserve in "expected" as the uitable package adds + // them during printing. + var expected = fmt.Sprintf(`URL CHART VERSION APP VERSION DESCRIPTION +%s/charts/stable/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend +%s/charts/bitnami/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend +`, ts.URL, ts.URL) + + testcmd := "search hub --endpoint " + ts.URL + " maria" + storage := storageFixture() + _, out, err := executeActionCommandC(storage, testcmd) + if err != nil { + t.Errorf("unexpected error, %s", err) + } + if out != expected { + t.Error("expected and actual output did not match") + t.Log(out) + t.Log(expected) + } +} + +func TestSearchHubListRepoCmd(t *testing.T) { + + // Setup a mock search service + var searchResult = `{"data":[{"id":"stable/phpmyadmin","type":"chart","attributes":{"name":"phpmyadmin","repo":{"name":"stable","url":"https://charts.helm.sh/stable"},"description":"phpMyAdmin is an mysql administration frontend","home":"https://www.phpmyadmin.net/","keywords":["mariadb","mysql","phpmyadmin"],"maintainers":[{"name":"Bitnami","email":"containers@bitnami.com"}],"sources":["https://github.com/bitnami/bitnami-docker-phpmyadmin"],"icon":""},"links":{"self":"/v1/charts/stable/phpmyadmin"},"relationships":{"latestChartVersion":{"data":{"version":"3.0.0","app_version":"4.9.0-1","created":"2019-08-08T17:57:31.38Z","digest":"119c499251bffd4b06ff0cd5ac98c2ce32231f84899fb4825be6c2d90971c742","urls":["https://charts.helm.sh/stable/phpmyadmin-3.0.0.tgz"],"readme":"/v1/assets/stable/phpmyadmin/versions/3.0.0/README.md","values":"/v1/assets/stable/phpmyadmin/versions/3.0.0/values.yaml"},"links":{"self":"/v1/charts/stable/phpmyadmin/versions/3.0.0"}}}},{"id":"bitnami/phpmyadmin","type":"chart","attributes":{"name":"phpmyadmin","repo":{"name":"bitnami","url":"https://charts.bitnami.com"},"description":"phpMyAdmin is an mysql administration frontend","home":"https://www.phpmyadmin.net/","keywords":["mariadb","mysql","phpmyadmin"],"maintainers":[{"name":"Bitnami","email":"containers@bitnami.com"}],"sources":["https://github.com/bitnami/bitnami-docker-phpmyadmin"],"icon":""},"links":{"self":"/v1/charts/bitnami/phpmyadmin"},"relationships":{"latestChartVersion":{"data":{"version":"3.0.0","app_version":"4.9.0-1","created":"2019-08-08T18:34:13.341Z","digest":"66d77cf6d8c2b52c488d0a294cd4996bd5bad8dc41d3829c394498fb401c008a","urls":["https://charts.bitnami.com/bitnami/phpmyadmin-3.0.0.tgz"],"readme":"/v1/assets/bitnami/phpmyadmin/versions/3.0.0/README.md","values":"/v1/assets/bitnami/phpmyadmin/versions/3.0.0/values.yaml"},"links":{"self":"/v1/charts/bitnami/phpmyadmin/versions/3.0.0"}}}}]}` + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, searchResult) + })) + defer ts.Close() + + // The expected output has the URL to the mocked search service in it + // Trailing spaces are necessary to preserve in "expected" as the uitable package adds + // them during printing. + var expected = fmt.Sprintf(`URL CHART VERSION APP VERSION DESCRIPTION REPO URL %s/charts/stable/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend https://charts.helm.sh/stable %s/charts/bitnami/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend https://charts.bitnami.com `, ts.URL, ts.URL) - testcmd := "search hub --endpoint " + ts.URL + " maria" + testcmd := "search hub --list-repo --endpoint " + ts.URL + " maria" storage := storageFixture() _, out, err := executeActionCommandC(storage, testcmd) if err != nil { From 5b101ec504f1c1190dd15134c74881a59a87f829 Mon Sep 17 00:00:00 2001 From: Dmitrii Ermakov Date: Sun, 15 Aug 2021 11:28:17 +0200 Subject: [PATCH 4/5] Reintroduce --list-repo flag in search repo Signed-off-by: Dmitrii Ermakov --- cmd/helm/search_hub.go | 21 ++++++++++++++++----- cmd/helm/search_hub_test.go | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/cmd/helm/search_hub.go b/cmd/helm/search_hub.go index 002d909ab..ed3f5bdf2 100644 --- a/cmd/helm/search_hub.go +++ b/cmd/helm/search_hub.go @@ -53,6 +53,7 @@ type searchHubOptions struct { searchEndpoint string maxColWidth uint outputFormat output.Format + listRepoURL bool } func newSearchHubCmd(out io.Writer) *cobra.Command { @@ -70,6 +71,7 @@ func newSearchHubCmd(out io.Writer) *cobra.Command { f := cmd.Flags() f.StringVar(&o.searchEndpoint, "endpoint", "https://hub.helm.sh", "Hub instance to query for charts") f.UintVar(&o.maxColWidth, "max-col-width", 50, "maximum column width for output table") + f.BoolVar(&o.listRepoURL, "list-repo", false, "print charts repository URL") bindOutputFlag(cmd, &o.outputFormat) @@ -89,7 +91,7 @@ func (o *searchHubOptions) run(out io.Writer, args []string) error { return fmt.Errorf("unable to perform search against %q", o.searchEndpoint) } - return o.outputFormat.Write(out, newHubSearchWriter(results, o.searchEndpoint, o.maxColWidth)) + return o.outputFormat.Write(out, newHubSearchWriter(results, o.searchEndpoint, o.maxColWidth, o.listRepoURL)) } type hubChartRepo struct { @@ -108,9 +110,10 @@ type hubChartElement struct { type hubSearchWriter struct { elements []hubChartElement columnWidth uint + listRepoURL bool } -func newHubSearchWriter(results []monocular.SearchResult, endpoint string, columnWidth uint) *hubSearchWriter { +func newHubSearchWriter(results []monocular.SearchResult, endpoint string, columnWidth uint, listRepoURL bool) *hubSearchWriter { var elements []hubChartElement for _, r := range results { // Backwards compatibility for Monocular @@ -123,7 +126,7 @@ func newHubSearchWriter(results []monocular.SearchResult, endpoint string, colum elements = append(elements, hubChartElement{url, r.Relationships.LatestChartVersion.Data.Version, r.Relationships.LatestChartVersion.Data.AppVersion, r.Attributes.Description, hubChartRepo{URL: r.Attributes.Repo.URL, Name: r.Attributes.Repo.Name}}) } - return &hubSearchWriter{elements, columnWidth} + return &hubSearchWriter{elements, columnWidth, listRepoURL} } func (h *hubSearchWriter) WriteTable(out io.Writer) error { @@ -137,10 +140,18 @@ func (h *hubSearchWriter) WriteTable(out io.Writer) error { table := uitable.New() table.MaxColWidth = h.columnWidth - table.AddRow("URL", "CHART VERSION", "APP VERSION", "DESCRIPTION", "REPO") + if h.listRepoURL { + table.AddRow("URL", "CHART VERSION", "APP VERSION", "DESCRIPTION", "REPO URL") + } else { + table.AddRow("URL", "CHART VERSION", "APP VERSION", "DESCRIPTION") + } for _, r := range h.elements { - table.AddRow(r.URL, r.Version, r.AppVersion, r.Description, r.Repository.URL) + if h.listRepoURL { + table.AddRow(r.URL, r.Version, r.AppVersion, r.Description, r.Repository.URL) + } else { + table.AddRow(r.URL, r.Version, r.AppVersion, r.Description) + } } return output.EncodeTable(out, table) } diff --git a/cmd/helm/search_hub_test.go b/cmd/helm/search_hub_test.go index 564c46775..3498845fa 100644 --- a/cmd/helm/search_hub_test.go +++ b/cmd/helm/search_hub_test.go @@ -33,12 +33,44 @@ func TestSearchHubCmd(t *testing.T) { defer ts.Close() // The expected output has the URL to the mocked search service in it - var expected = fmt.Sprintf(`URL CHART VERSION APP VERSION DESCRIPTION REPO + // Trailing spaces are necessary to preserve in "expected" as the uitable package adds + // them during printing. + var expected = fmt.Sprintf(`URL CHART VERSION APP VERSION DESCRIPTION +%s/charts/stable/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend +%s/charts/bitnami/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend +`, ts.URL, ts.URL) + + testcmd := "search hub --endpoint " + ts.URL + " maria" + storage := storageFixture() + _, out, err := executeActionCommandC(storage, testcmd) + if err != nil { + t.Errorf("unexpected error, %s", err) + } + if out != expected { + t.Error("expected and actual output did not match") + t.Log(out) + t.Log(expected) + } +} + +func TestSearchHubListRepoCmd(t *testing.T) { + + // Setup a mock search service + var searchResult = `{"data":[{"id":"stable/phpmyadmin","type":"chart","attributes":{"name":"phpmyadmin","repo":{"name":"stable","url":"https://charts.helm.sh/stable"},"description":"phpMyAdmin is an mysql administration frontend","home":"https://www.phpmyadmin.net/","keywords":["mariadb","mysql","phpmyadmin"],"maintainers":[{"name":"Bitnami","email":"containers@bitnami.com"}],"sources":["https://github.com/bitnami/bitnami-docker-phpmyadmin"],"icon":""},"links":{"self":"/v1/charts/stable/phpmyadmin"},"relationships":{"latestChartVersion":{"data":{"version":"3.0.0","app_version":"4.9.0-1","created":"2019-08-08T17:57:31.38Z","digest":"119c499251bffd4b06ff0cd5ac98c2ce32231f84899fb4825be6c2d90971c742","urls":["https://charts.helm.sh/stable/phpmyadmin-3.0.0.tgz"],"readme":"/v1/assets/stable/phpmyadmin/versions/3.0.0/README.md","values":"/v1/assets/stable/phpmyadmin/versions/3.0.0/values.yaml"},"links":{"self":"/v1/charts/stable/phpmyadmin/versions/3.0.0"}}}},{"id":"bitnami/phpmyadmin","type":"chart","attributes":{"name":"phpmyadmin","repo":{"name":"bitnami","url":"https://charts.bitnami.com"},"description":"phpMyAdmin is an mysql administration frontend","home":"https://www.phpmyadmin.net/","keywords":["mariadb","mysql","phpmyadmin"],"maintainers":[{"name":"Bitnami","email":"containers@bitnami.com"}],"sources":["https://github.com/bitnami/bitnami-docker-phpmyadmin"],"icon":""},"links":{"self":"/v1/charts/bitnami/phpmyadmin"},"relationships":{"latestChartVersion":{"data":{"version":"3.0.0","app_version":"4.9.0-1","created":"2019-08-08T18:34:13.341Z","digest":"66d77cf6d8c2b52c488d0a294cd4996bd5bad8dc41d3829c394498fb401c008a","urls":["https://charts.bitnami.com/bitnami/phpmyadmin-3.0.0.tgz"],"readme":"/v1/assets/bitnami/phpmyadmin/versions/3.0.0/README.md","values":"/v1/assets/bitnami/phpmyadmin/versions/3.0.0/values.yaml"},"links":{"self":"/v1/charts/bitnami/phpmyadmin/versions/3.0.0"}}}}]}` + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, searchResult) + })) + defer ts.Close() + + // The expected output has the URL to the mocked search service in it + // Trailing spaces are necessary to preserve in "expected" as the uitable package adds + // them during printing. + var expected = fmt.Sprintf(`URL CHART VERSION APP VERSION DESCRIPTION REPO URL %s/charts/stable/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend https://charts.helm.sh/stable %s/charts/bitnami/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend https://charts.bitnami.com `, ts.URL, ts.URL) - testcmd := "search hub --endpoint " + ts.URL + " maria" + testcmd := "search hub --list-repo --endpoint " + ts.URL + " maria" storage := storageFixture() _, out, err := executeActionCommandC(storage, testcmd) if err != nil { From 144048d5fa95ad9e7cef5af2e735b01802e613b2 Mon Sep 17 00:00:00 2001 From: Dmitrii Ermakov Date: Sun, 29 Aug 2021 20:38:13 +0200 Subject: [PATCH 5/5] Replace cli flag --list-repo with --list-repo-url Signed-off-by: Dmitrii Ermakov --- cmd/helm/search_hub.go | 2 +- cmd/helm/search_hub_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/helm/search_hub.go b/cmd/helm/search_hub.go index ed3f5bdf2..b8887efd5 100644 --- a/cmd/helm/search_hub.go +++ b/cmd/helm/search_hub.go @@ -71,7 +71,7 @@ func newSearchHubCmd(out io.Writer) *cobra.Command { f := cmd.Flags() f.StringVar(&o.searchEndpoint, "endpoint", "https://hub.helm.sh", "Hub instance to query for charts") f.UintVar(&o.maxColWidth, "max-col-width", 50, "maximum column width for output table") - f.BoolVar(&o.listRepoURL, "list-repo", false, "print charts repository URL") + f.BoolVar(&o.listRepoURL, "list-repo-url", false, "print charts repository URL") bindOutputFlag(cmd, &o.outputFormat) diff --git a/cmd/helm/search_hub_test.go b/cmd/helm/search_hub_test.go index 60dd4aae2..7df54ea8f 100644 --- a/cmd/helm/search_hub_test.go +++ b/cmd/helm/search_hub_test.go @@ -70,7 +70,7 @@ func TestSearchHubListRepoCmd(t *testing.T) { %s/charts/bitnami/phpmyadmin 3.0.0 4.9.0-1 phpMyAdmin is an mysql administration frontend https://charts.bitnami.com `, ts.URL, ts.URL) - testcmd := "search hub --list-repo --endpoint " + ts.URL + " maria" + testcmd := "search hub --list-repo-url --endpoint " + ts.URL + " maria" storage := storageFixture() _, out, err := executeActionCommandC(storage, testcmd) if err != nil {