From e1302a302314cb6b7a5558c0b8c36ce722d5cc20 Mon Sep 17 00:00:00 2001 From: Alexandre Fourcat Date: Sun, 10 Jan 2021 18:59:38 +0900 Subject: Adding json formatting to `--list-tags` option in `podman search` command. Data is formatted following this JSON structure: ```json { "Name": "...", "Tags": ["...", "...", "..."] } ``` Closes: #8740. Signed-off-by: Alexandre Fourcat --- cmd/podman/images/search.go | 47 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'cmd/podman') diff --git a/cmd/podman/images/search.go b/cmd/podman/images/search.go index c2ef7d767..e4d325361 100644 --- a/cmd/podman/images/search.go +++ b/cmd/podman/images/search.go @@ -149,14 +149,13 @@ func imageSearch(cmd *cobra.Command, args []string) error { if len(searchOptions.Filters) != 0 { return errors.Errorf("filters are not applicable to list tags result") } + if report.IsJSON(searchOptions.Format) { + listTagsEntries := buildListTagsJson(searchReport) + return printJson(listTagsEntries) + } row = "{{.Name}}\t{{.Tag}}\n" case report.IsJSON(searchOptions.Format): - prettyJSON, err := json.MarshalIndent(searchReport, "", " ") - if err != nil { - return err - } - fmt.Println(string(prettyJSON)) - return nil + return printJson(searchReport) case cmd.Flags().Changed("format"): renderHeaders = parse.HasTable(searchOptions.Format) row = report.NormalizeFormat(searchOptions.Format) @@ -180,3 +179,39 @@ func imageSearch(cmd *cobra.Command, args []string) error { return tmpl.Execute(w, searchReport) } + +func printJson(v interface{}) error { + prettyJSON, err := json.MarshalIndent(v, "", " ") + if err != nil { + return err + } + fmt.Println(string(prettyJSON)) + return nil +} + +func buildListTagsJson(searchReport []entities.ImageSearchReport) interface{} { + entries := []struct { + Name string + Tags []string + }{} + +ReportLoop: + for _, report := range searchReport { + for idx, entry := range entries { + if entry.Name == report.Name { + entries[idx].Tags = append(entries[idx].Tags, report.Tag) + continue ReportLoop + } + } + newElem := struct { + Name string + Tags []string + }{ + report.Name, + []string{report.Tag}, + } + + entries = append(entries, newElem) + } + return entries +} -- cgit v1.2.3-54-g00ecf From 95462e802a53e7fee38847b0b66200a8edc8a4ba Mon Sep 17 00:00:00 2001 From: Alexandre Fourcat Date: Sun, 10 Jan 2021 21:27:04 +0900 Subject: Better test and idomatic code. Adding another check in the `podman search --list-tags --format json` test case. Replacing an anonymous struct by \`listEntryTag\` struct. Signed-off-by: Alexandre Fourcat --- cmd/podman/images/search.go | 18 +++++++++--------- test/e2e/search_test.go | 1 + 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'cmd/podman') diff --git a/cmd/podman/images/search.go b/cmd/podman/images/search.go index e4d325361..c8ea4b04a 100644 --- a/cmd/podman/images/search.go +++ b/cmd/podman/images/search.go @@ -26,6 +26,12 @@ type searchOptionsWrapper struct { Format string // For go templating } +// listEntryTag is a utility structure used for json serialization. +type listEntryTag struct { + Name string + Tags []string +} + var ( searchOptions = searchOptionsWrapper{} searchDescription = `Search registries for a given image. Can search all the default registries or a specific registry. @@ -189,11 +195,8 @@ func printJson(v interface{}) error { return nil } -func buildListTagsJson(searchReport []entities.ImageSearchReport) interface{} { - entries := []struct { - Name string - Tags []string - }{} +func buildListTagsJson(searchReport []entities.ImageSearchReport) []listEntryTag { + entries := []listEntryTag{} ReportLoop: for _, report := range searchReport { @@ -203,10 +206,7 @@ ReportLoop: continue ReportLoop } } - newElem := struct { - Name string - Tags []string - }{ + newElem := listEntryTag{ report.Name, []string{report.Tag}, } diff --git a/test/e2e/search_test.go b/test/e2e/search_test.go index 4a04a88cd..1d86ae744 100644 --- a/test/e2e/search_test.go +++ b/test/e2e/search_test.go @@ -131,6 +131,7 @@ registries = ['{{.Host}}:{{.Port}}']` Expect(search.IsJSONOutputValid()).To(BeTrue()) Expect(search.OutputToString()).To(ContainSubstring("docker.io/library/alpine")) Expect(search.OutputToString()).To(ContainSubstring("3.10")) + Expect(search.OutputToString()).To(ContainSubstring("2.7")) }) It("podman search no-trunc flag", func() { -- cgit v1.2.3-54-g00ecf