diff --git a/cmd/helm/search.go b/cmd/helm/search.go index 7db476b3a..631f39140 100644 --- a/cmd/helm/search.go +++ b/cmd/helm/search.go @@ -166,10 +166,12 @@ func (s *searchCmd) formatSearchResults(res []*search.Result, colWidth uint) str } func (s *searchCmd) formatSearchResultsJson(res []*search.Result, colWidth uint) (string, error) { + var resultJson []ResultEntry + if len(res) == 0 { return "No results found", nil } - resultJson := []ResultEntry{} + for _, r := range res { resultRow := ResultEntry{ Name: r.Name, @@ -179,12 +181,12 @@ func (s *searchCmd) formatSearchResultsJson(res []*search.Result, colWidth uint) } resultJson = append(resultJson, resultRow) } - json, err := json.MarshalIndent(Results{SearchResults: resultJson}, "", " ") + jsonSearchResults, err := json.MarshalIndent(Results{SearchResults: resultJson}, "", " ") if err != nil { return "", err } - return string(json), nil + return string(jsonSearchResults), nil } func (s *searchCmd) buildIndex() (*search.Index, error) { diff --git a/cmd/helm/search_test.go b/cmd/helm/search_test.go index 6a1e84f24..4617ea151 100644 --- a/cmd/helm/search_test.go +++ b/cmd/helm/search_test.go @@ -85,10 +85,10 @@ func TestSearchCmd(t *testing.T) { err: true, }, { - name: "search for 'alpine', expect two matches in json", - args: []string{"alpine"}, - flags: []string{"--output", "json"}, - expected: "[\n {\n \"name\": \"testing/alpine\",\n \"chartVersion\": \"0.2.0\",\n \"appVersion\": \"2.3.4\",\n \"description\": \"Deploy a basic Alpine Linux pod\"\n }\n]\n", + name: "search for 'alpine', expect two matches in json", + args: []string{"alpine"}, + flags: []string{"--output", "json"}, + expected: "{\n \"searchResults\": \\[\n {\n \"name\": \"testing/alpine\",\n \"chartVersion\": \"0.2.0\",\n \"appVersion\": \"2.3.4\",\n \"description\": \"Deploy a basic Alpine Linux pod\"\n }\n \\]\n}", }, }