Commit 79e5fd6b authored by Matt Butcher's avatar Matt Butcher

fix(helm): fix 'helm search' to use UITable

Closes #1261
parent 9f3dea90
......@@ -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) {
......
......@@ -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
......
......@@ -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,
},
{
......
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