diff options
author | Alexandre Fourcat <afourcat@gmail.com> | 2021-01-10 18:59:38 +0900 |
---|---|---|
committer | Alexandre Fourcat <afourcat@gmail.com> | 2021-01-10 19:04:23 +0900 |
commit | e1302a302314cb6b7a5558c0b8c36ce722d5cc20 (patch) | |
tree | f2f02772db068965e023f684416ce74a2e7b929d /cmd | |
parent | 49db79e735acd2c693762eaff62680cd9a8cb60b (diff) | |
download | podman-e1302a302314cb6b7a5558c0b8c36ce722d5cc20.tar.gz podman-e1302a302314cb6b7a5558c0b8c36ce722d5cc20.tar.bz2 podman-e1302a302314cb6b7a5558c0b8c36ce722d5cc20.zip |
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 <afourcat@gmail.com>
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/podman/images/search.go | 47 |
1 files changed, 41 insertions, 6 deletions
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 +} |