From e0e6a7acb6090d1b248a6aa1126a54a140537c23 Mon Sep 17 00:00:00 2001 From: Art Begolli Date: Fri, 26 Jul 2019 16:06:10 +0100 Subject: [PATCH 1/3] feat(): initial json output functionality --- cmd/helm/search.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/cmd/helm/search.go b/cmd/helm/search.go index 99ffafbd3..79ff91f97 100644 --- a/cmd/helm/search.go +++ b/cmd/helm/search.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "encoding/json" "fmt" "io" "strings" @@ -48,6 +49,14 @@ type searchCmd struct { regexp bool version string colWidth uint + output string +} + +type ResultEntry struct { + Name string `json:"name"` + ChartVersion string `json:"chartVersion"` + AppVersion string `json:"appVersion"` + Description string `json:"description"` } func newSearchCmd(out io.Writer) *cobra.Command { @@ -68,6 +77,7 @@ func newSearchCmd(out io.Writer) *cobra.Command { f.BoolVarP(&sc.versions, "versions", "l", false, "Show the long listing, with each version of each chart on its own line") f.StringVarP(&sc.version, "version", "v", "", "Search using semantic versioning constraints") f.UintVar(&sc.colWidth, "col-width", 60, "Specifies the max column width of output") + f.StringVarP(&sc.output, "output", "o", "", "Show the output in specified format") return cmd } @@ -95,6 +105,12 @@ func (s *searchCmd) run(args []string) error { return err } + if s.output == "json" { + formattedResults, _ := s.formatSearchResultsJson(data, s.colWidth) + fmt.Fprintln(s.out, formattedResults) + return nil + } + fmt.Fprintln(s.out, s.formatSearchResults(data, s.colWidth)) return nil @@ -141,6 +157,28 @@ func (s *searchCmd) formatSearchResults(res []*search.Result, colWidth uint) str return table.String() } +func (s *searchCmd) formatSearchResultsJson(res []*search.Result, colWidth uint) (string, error) { + if len(res) == 0 { + return "No results found", nil + } + resultJson := []ResultEntry{} + for _, r := range res { + resultRow := ResultEntry{ + Name: r.Name, + ChartVersion: r.Chart.Version, + AppVersion: r.Chart.AppVersion, + Description: r.Chart.Description, + } + resultJson = append(resultJson, resultRow) + } + json, err := json.MarshalIndent(resultJson, "", " ") + if err != nil { + return "", err + } + + return string(json), nil +} + func (s *searchCmd) buildIndex() (*search.Index, error) { // Load the repositories.yaml rf, err := repo.LoadRepositoriesFile(s.helmhome.RepositoryFile()) From f3d57f6f24ba86da08fc6e20bc2688509c65e6ae Mon Sep 17 00:00:00 2001 From: Art Begolli Date: Fri, 26 Jul 2019 17:49:07 +0100 Subject: [PATCH 2/3] feat(): adding unit test for json flag --- cmd/helm/search_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmd/helm/search_test.go b/cmd/helm/search_test.go index 233f94572..6a1e84f24 100644 --- a/cmd/helm/search_test.go +++ b/cmd/helm/search_test.go @@ -84,6 +84,12 @@ func TestSearchCmd(t *testing.T) { flags: []string{"--regexp"}, 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", + }, } cleanup := resetEnv() From b27d6bb9109747b5f5a3e26ea201ff90bbfc9ee3 Mon Sep 17 00:00:00 2001 From: Art Begolli Date: Fri, 26 Jul 2019 17:57:19 +0100 Subject: [PATCH 3/3] feat(): updating array to json --- cmd/helm/search.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/helm/search.go b/cmd/helm/search.go index 79ff91f97..1a136cb2f 100644 --- a/cmd/helm/search.go +++ b/cmd/helm/search.go @@ -59,6 +59,10 @@ type ResultEntry struct { Description string `json:"description"` } +type Results struct { + SearchResults []ResultEntry `json:"searchResults"` +} + func newSearchCmd(out io.Writer) *cobra.Command { sc := &searchCmd{out: out} @@ -171,7 +175,7 @@ func (s *searchCmd) formatSearchResultsJson(res []*search.Result, colWidth uint) } resultJson = append(resultJson, resultRow) } - json, err := json.MarshalIndent(resultJson, "", " ") + json, err := json.MarshalIndent(Results{SearchResults: resultJson}, "", " ") if err != nil { return "", err }