diff options
author | Jhon Honce <jhonce@redhat.com> | 2021-11-18 09:03:00 -0700 |
---|---|---|
committer | Jhon Honce <jhonce@redhat.com> | 2021-12-02 09:07:27 -0700 |
commit | 61792de36ea2ea98f6e3aef3821d1b15beebf9e0 (patch) | |
tree | f2e73122b2cdb0323e1d38792eef44a625b85d10 /cmd/podman/images | |
parent | fbcebcb46a2444ce078dee10d8d51555bee8f177 (diff) | |
download | podman-61792de36ea2ea98f6e3aef3821d1b15beebf9e0.tar.gz podman-61792de36ea2ea98f6e3aef3821d1b15beebf9e0.tar.bz2 podman-61792de36ea2ea98f6e3aef3821d1b15beebf9e0.zip |
Refactor podman image command output
Leverage new report.Formatter allowing better compatibility from
podman command output.
Follow on PR's will cover containers, etc.
See #10974
Signed-off-by: Jhon Honce <jhonce@redhat.com>
Diffstat (limited to 'cmd/podman/images')
-rw-r--r-- | cmd/podman/images/history.go | 36 | ||||
-rw-r--r-- | cmd/podman/images/list.go | 33 | ||||
-rw-r--r-- | cmd/podman/images/mount.go | 15 | ||||
-rw-r--r-- | cmd/podman/images/search.go | 35 | ||||
-rw-r--r-- | cmd/podman/images/trust_show.go | 27 |
5 files changed, 54 insertions, 92 deletions
diff --git a/cmd/podman/images/history.go b/cmd/podman/images/history.go index cc7b1b4eb..ac693a87b 100644 --- a/cmd/podman/images/history.go +++ b/cmd/podman/images/history.go @@ -1,7 +1,6 @@ package images import ( - "context" "fmt" "os" "strings" @@ -79,7 +78,7 @@ func historyFlags(cmd *cobra.Command) { } func history(cmd *cobra.Command, args []string) error { - results, err := registry.ImageEngine().History(context.Background(), args[0], entities.ImageHistoryOptions{}) + results, err := registry.ImageEngine().History(registry.Context(), args[0], entities.ImageHistoryOptions{}) if err != nil { return err } @@ -111,37 +110,32 @@ func history(cmd *cobra.Command, args []string) error { hr = append(hr, historyReporter{l}) } - hdrs := report.Headers(historyReporter{}, map[string]string{ - "CreatedBy": "CREATED BY", - }) + rpt := report.New(os.Stdout, cmd.Name()) + defer rpt.Flush() - // Defaults - row := "{{.ID}}\t{{.Created}}\t{{.CreatedBy}}\t{{.Size}}\t{{.Comment}}\n" switch { - case cmd.Flags().Changed("format"): - row = report.NormalizeFormat(opts.format) case opts.quiet: - row = "{{.ID}}\n" + rpt, err = rpt.Parse(report.OriginUser, "{{range .}}{{.ID}}\n{{end -}}") + case cmd.Flags().Changed("format"): + rpt, err = rpt.Parse(report.OriginUser, cmd.Flag("format").Value.String()) + default: + format := "{{range .}}{{.ID}}\t{{.Created}}\t{{.CreatedBy}}\t{{.Size}}\t{{.Comment}}\n{{end -}}" + rpt, err = rpt.Parse(report.OriginPodman, format) } - format := report.EnforceRange(row) - - tmpl, err := report.NewTemplate("history").Parse(format) if err != nil { return err } - w, err := report.NewWriterDefault(os.Stdout) - if err != nil { - return err - } - defer w.Flush() + if rpt.RenderHeaders { + hdrs := report.Headers(historyReporter{}, map[string]string{ + "CreatedBy": "CREATED BY", + }) - if !opts.quiet && !cmd.Flags().Changed("format") { - if err := tmpl.Execute(w, hdrs); err != nil { + if err := rpt.Execute(hdrs); err != nil { return errors.Wrapf(err, "failed to write report column headers") } } - return tmpl.Execute(w, hr) + return rpt.Execute(hr) } type historyReporter struct { diff --git a/cmd/podman/images/list.go b/cmd/podman/images/list.go index 01286daf2..61514daa7 100644 --- a/cmd/podman/images/list.go +++ b/cmd/podman/images/list.go @@ -117,7 +117,7 @@ func images(cmd *cobra.Command, args []string) error { listOptions.Filter = append(listOptions.Filter, "reference="+args[0]) } - if cmd.Flag("sort").Changed && !sortFields.Contains(listFlag.sort) { + if cmd.Flags().Changed("sort") && !sortFields.Contains(listFlag.sort) { return fmt.Errorf("\"%s\" is not a valid field for sorting. Choose from: %s", listFlag.sort, sortFields.String()) } @@ -140,7 +140,7 @@ func images(cmd *cobra.Command, args []string) error { if cmd.Flags().Changed("format") && !report.HasTable(listFlag.format) { listFlag.noHeading = true } - return writeTemplate(imgs) + return writeTemplate(cmd, imgs) } } @@ -186,38 +186,31 @@ func writeJSON(images []imageReporter) error { return nil } -func writeTemplate(imgs []imageReporter) error { +func writeTemplate(cmd *cobra.Command, imgs []imageReporter) error { hdrs := report.Headers(imageReporter{}, map[string]string{ "ID": "IMAGE ID", "ReadOnly": "R/O", }) - var format string - if listFlag.format == "" { - format = lsFormatFromFlags(listFlag) - } else { - format = report.NormalizeFormat(listFlag.format) - format = report.EnforceRange(format) - } + rpt := report.New(os.Stdout, cmd.Name()) + defer rpt.Flush() - tmpl, err := report.NewTemplate("list").Parse(format) - if err != nil { - return err + var err error + if cmd.Flags().Changed("format") { + rpt, err = rpt.Parse(report.OriginUser, cmd.Flag("format").Value.String()) + } else { + rpt, err = rpt.Parse(report.OriginPodman, lsFormatFromFlags(listFlag)) } - - w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - defer w.Flush() - if !listFlag.noHeading { - if err := tmpl.Execute(w, hdrs); err != nil { + if rpt.RenderHeaders && !listFlag.noHeading { + if err := rpt.Execute(hdrs); err != nil { return err } } - - return tmpl.Execute(w, imgs) + return rpt.Execute(imgs) } func sortImages(imageS []*entities.ImageSummary) ([]imageReporter, error) { diff --git a/cmd/podman/images/mount.go b/cmd/podman/images/mount.go index 89c00cb70..cdeb9eecf 100644 --- a/cmd/podman/images/mount.go +++ b/cmd/podman/images/mount.go @@ -87,7 +87,7 @@ func mount(cmd *cobra.Command, args []string) error { case report.IsJSON(mountOpts.Format): return printJSON(reports) case mountOpts.Format == "": - break // default format + break // see default format below default: return errors.Errorf("unknown --format argument: %q", mountOpts.Format) } @@ -97,19 +97,12 @@ func mount(cmd *cobra.Command, args []string) error { mrs = append(mrs, mountReporter{r}) } - row := "{{range . }}{{.ID}}\t{{.Path}}\n{{end -}}" - tmpl, err := report.NewTemplate("mounts").Parse(row) + rpt, err := report.New(os.Stdout, cmd.Name()).Parse(report.OriginPodman, "{{range . }}{{.ID}}\t{{.Path}}\n{{end -}}") if err != nil { return err } - - w, err := report.NewWriterDefault(os.Stdout) - if err != nil { - return err - } - defer w.Flush() - - return tmpl.Execute(w, mrs) + defer rpt.Flush() + return rpt.Execute(mrs) } func printJSON(reports []*entities.ImageMountReport) error { diff --git a/cmd/podman/images/search.go b/cmd/podman/images/search.go index c9a4793aa..0791ac02c 100644 --- a/cmd/podman/images/search.go +++ b/cmd/podman/images/search.go @@ -149,9 +149,9 @@ func imageSearch(cmd *cobra.Command, args []string) error { searchReport[i].Description = d } - hdrs := report.Headers(entities.ImageSearchReport{}, nil) - renderHeaders := true - var row string + rpt := report.New(os.Stdout, cmd.Name()) + defer rpt.Flush() + switch { case searchOptions.ListTags: if len(searchOptions.Filters) != 0 { @@ -161,39 +161,30 @@ func imageSearch(cmd *cobra.Command, args []string) error { listTagsEntries := buildListTagsJSON(searchReport) return printArbitraryJSON(listTagsEntries) } - row = "{{.Name}}\t{{.Tag}}\n" + rpt, err = rpt.Parse(report.OriginPodman, "{{range .}}{{.Name}}\t{{.Tag}}\n{{end -}}") case isJSON: return printArbitraryJSON(searchReport) case cmd.Flags().Changed("format"): - renderHeaders = report.HasTable(searchOptions.Format) - row = report.NormalizeFormat(searchOptions.Format) + rpt, err = rpt.Parse(report.OriginUser, searchOptions.Format) default: - row = "{{.Name}}\t{{.Description}}" + row := "{{.Name}}\t{{.Description}}" if searchOptions.Compatible { row += "\t{{.Stars}}\t{{.Official}}\t{{.Automated}}" } - row += "\n" + row = "{{range . }}" + row + "\n{{end -}}" + rpt, err = rpt.Parse(report.OriginPodman, row) } - format := report.EnforceRange(row) - - tmpl, err := report.NewTemplate("search").Parse(format) if err != nil { return err } - w, err := report.NewWriterDefault(os.Stdout) - if err != nil { - return err - } - defer w.Flush() - - if renderHeaders { - if err := tmpl.Execute(w, hdrs); err != nil { - return errors.Wrapf(err, "failed to write search column headers") + if rpt.RenderHeaders { + hdrs := report.Headers(entities.ImageSearchReport{}, nil) + if err := rpt.Execute(hdrs); err != nil { + return errors.Wrapf(err, "failed to write report column headers") } } - - return tmpl.Execute(w, searchReport) + return rpt.Execute(searchReport) } func printArbitraryJSON(v interface{}) error { diff --git a/cmd/podman/images/trust_show.go b/cmd/podman/images/trust_show.go index c0e56f504..04ea24ca5 100644 --- a/cmd/podman/images/trust_show.go +++ b/cmd/podman/images/trust_show.go @@ -48,11 +48,12 @@ func showTrust(cmd *cobra.Command, args []string) error { if err != nil { return err } - if showTrustOptions.Raw { + + switch { + case showTrustOptions.Raw: fmt.Println(string(trust.Raw)) return nil - } - if showTrustOptions.JSON { + case showTrustOptions.JSON: b, err := json.MarshalIndent(trust.Policies, "", " ") if err != nil { return err @@ -60,23 +61,13 @@ func showTrust(cmd *cobra.Command, args []string) error { fmt.Println(string(b)) return nil } + rpt := report.New(os.Stdout, cmd.Name()) + defer rpt.Flush() - format := "{{range . }}{{.RepoName}}\t{{.Type}}\t{{.GPGId}}\t{{.SignatureStore}}\n{{end -}}" - tmpl, err := report.NewTemplate("list").Parse(format) - if err != nil { - return err - } - - w, err := report.NewWriterDefault(os.Stdout) + rpt, err = rpt.Parse(report.OriginPodman, + "{{range . }}{{.RepoName}}\t{{.Type}}\t{{.GPGId}}\t{{.SignatureStore}}\n{{end -}}") if err != nil { return err } - - if err := tmpl.Execute(w, trust.Policies); err != nil { - return err - } - if err := w.Flush(); err != nil { - return err - } - return nil + return rpt.Execute(trust.Policies) } |