Merge pull request #8923 from Afourcat/master

Adding json formatting to `--list-tags` option in `podman search` command.
This commit is contained in:
OpenShift Merge Robot
2021-01-12 10:29:21 -05:00
committed by GitHub
2 changed files with 51 additions and 6 deletions

View File

@ -26,6 +26,12 @@ type searchOptionsWrapper struct {
Format string // For go templating Format string // For go templating
} }
// listEntryTag is a utility structure used for json serialization.
type listEntryTag struct {
Name string
Tags []string
}
var ( var (
searchOptions = searchOptionsWrapper{} searchOptions = searchOptionsWrapper{}
searchDescription = `Search registries for a given image. Can search all the default registries or a specific registry. searchDescription = `Search registries for a given image. Can search all the default registries or a specific registry.
@ -149,14 +155,13 @@ func imageSearch(cmd *cobra.Command, args []string) error {
if len(searchOptions.Filters) != 0 { if len(searchOptions.Filters) != 0 {
return errors.Errorf("filters are not applicable to list tags result") 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" row = "{{.Name}}\t{{.Tag}}\n"
case report.IsJSON(searchOptions.Format): case report.IsJSON(searchOptions.Format):
prettyJSON, err := json.MarshalIndent(searchReport, "", " ") return printJson(searchReport)
if err != nil {
return err
}
fmt.Println(string(prettyJSON))
return nil
case cmd.Flags().Changed("format"): case cmd.Flags().Changed("format"):
renderHeaders = parse.HasTable(searchOptions.Format) renderHeaders = parse.HasTable(searchOptions.Format)
row = report.NormalizeFormat(searchOptions.Format) row = report.NormalizeFormat(searchOptions.Format)
@ -180,3 +185,33 @@ func imageSearch(cmd *cobra.Command, args []string) error {
return tmpl.Execute(w, searchReport) 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) []listEntryTag {
entries := []listEntryTag{}
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 := listEntryTag{
report.Name,
[]string{report.Tag},
}
entries = append(entries, newElem)
}
return entries
}

View File

@ -124,6 +124,16 @@ registries = ['{{.Host}}:{{.Port}}']`
Expect(search.OutputToString()).To(ContainSubstring("docker.io/library/alpine")) Expect(search.OutputToString()).To(ContainSubstring("docker.io/library/alpine"))
}) })
It("podman search format json list tags", func() {
search := podmanTest.Podman([]string{"search", "--list-tags", "--format", "json", "alpine"})
search.WaitWithDefaultTimeout()
Expect(search.ExitCode()).To(Equal(0))
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() { It("podman search no-trunc flag", func() {
search := podmanTest.Podman([]string{"search", "--no-trunc", "alpine"}) search := podmanTest.Podman([]string{"search", "--no-trunc", "alpine"})
search.WaitWithDefaultTimeout() search.WaitWithDefaultTimeout()