Skip to content

Commit

Permalink
Adding json formatting to --list-tags option in podman search
Browse files Browse the repository at this point in the history
command.

Data is formatted following this JSON structure:
```json
{
    "Name": "...",
    "Tags": ["...", "...", "..."]
}
```

Closes: containers#8740.

Signed-off-by: Alexandre Fourcat <[email protected]>
  • Loading branch information
Afourcat committed Jan 10, 2021
1 parent 49db79e commit e1302a3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
47 changes: 41 additions & 6 deletions cmd/podman/images/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
9 changes: 9 additions & 0 deletions test/e2e/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ registries = ['{{.Host}}:{{.Port}}']`
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"))
})

It("podman search no-trunc flag", func() {
search := podmanTest.Podman([]string{"search", "--no-trunc", "alpine"})
search.WaitWithDefaultTimeout()
Expand Down

0 comments on commit e1302a3

Please sign in to comment.