From 79e5fd6b74abface96e3df2feeb17b713cfab6e0 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Tue, 4 Oct 2016 21:19:52 -0600 Subject: [PATCH] fix(helm): fix 'helm search' to use UITable Closes #1261 --- cmd/helm/search.go | 29 ++++++++++++++++++++++++----- cmd/helm/search/search.go | 5 +++-- cmd/helm/search_test.go | 8 ++++---- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/cmd/helm/search.go b/cmd/helm/search.go index f8b02cff8..264870810 100644 --- a/cmd/helm/search.go +++ b/cmd/helm/search.go @@ -21,6 +21,7 @@ import ( "io" "strings" + "github.com/gosuri/uitable" "github.com/spf13/cobra" "k8s.io/helm/cmd/helm/helmpath" @@ -71,6 +72,7 @@ func (s *searchCmd) run(args []string) error { if len(args) == 0 { s.showAllCharts(index) + return nil } q := strings.Join(args, " ") @@ -80,17 +82,34 @@ func (s *searchCmd) run(args []string) error { } search.SortScore(res) - for _, r := range res { - fmt.Fprintln(s.out, r.Name) - } + fmt.Fprintln(s.out, s.formatSearchResults(res)) return nil } func (s *searchCmd) showAllCharts(i *search.Index) { - for name := range i.Entries() { - fmt.Fprintln(s.out, name) + e := i.Entries() + res := make([]*search.Result, len(e)) + j := 0 + for name, ch := range e { + res[j] = &search.Result{ + Name: name, + Chart: ch, + } + j++ + } + search.SortScore(res) + fmt.Fprintln(s.out, s.formatSearchResults(res)) +} + +func (s *searchCmd) formatSearchResults(res []*search.Result) string { + table := uitable.New() + table.MaxColWidth = 50 + table.AddRow("NAME", "VERSION", "DESCRIPTION") + for _, r := range res { + table.AddRow(r.Name, r.Chart.Version, r.Chart.Description) } + return table.String() } func (s *searchCmd) buildIndex() (*search.Index, error) { diff --git a/cmd/helm/search/search.go b/cmd/helm/search/search.go index def6da698..aa17fd7ee 100644 --- a/cmd/helm/search/search.go +++ b/cmd/helm/search/search.go @@ -39,6 +39,7 @@ import ( type Result struct { Name string Score int + Chart *repo.ChartVersion } // Index is a searchable index of chart information. @@ -117,7 +118,7 @@ func (i *Index) SearchLiteral(term string, threshold int) []*Result { for k, v := range i.lines { res := strings.Index(v, term) if score := i.calcScore(res, v); res != -1 && score < threshold { - buf = append(buf, &Result{Name: k, Score: score}) + buf = append(buf, &Result{Name: k, Score: score, Chart: i.charts[k]}) } } return buf @@ -136,7 +137,7 @@ func (i *Index) SearchRegexp(re string, threshold int) ([]*Result, error) { continue } if score := i.calcScore(ind[0], v); ind[0] >= 0 && score < threshold { - buf = append(buf, &Result{Name: k, Score: score}) + buf = append(buf, &Result{Name: k, Score: score, Chart: i.charts[k]}) } } return buf, nil diff --git a/cmd/helm/search_test.go b/cmd/helm/search_test.go index c9ce62e05..40eb935d3 100644 --- a/cmd/helm/search_test.go +++ b/cmd/helm/search_test.go @@ -34,23 +34,23 @@ func TestSearchCmd(t *testing.T) { { name: "search for 'maria', expect one match", args: []string{"maria"}, - expect: "testing/mariadb", + expect: "NAME \tVERSION\tDESCRIPTION \ntesting/mariadb\t0.3.0 \tChart for MariaDB", }, { name: "search for 'alpine', expect two matches", args: []string{"alpine"}, - expect: "testing/alpine", + expect: "NAME \tVERSION\tDESCRIPTION \ntesting/alpine\t0.1.0 \tDeploy a basic Alpine Linux pod", }, { name: "search for 'syzygy', expect no matches", args: []string{"syzygy"}, - expect: "", + expect: "NAME\tVERSION\tDESCRIPTION", }, { name: "search for 'alp[a-z]+', expect two matches", args: []string{"alp[a-z]+"}, flags: []string{"--regexp"}, - expect: "testing/alpine", + expect: "NAME \tVERSION\tDESCRIPTION \ntesting/alpine\t0.1.0 \tDeploy a basic Alpine Linux pod", regexp: true, }, {