diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2021-12-01 22:05:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-01 22:05:12 +0100 |
commit | 1422cdbac192c7c9c907f88f11969c028d1e9611 (patch) | |
tree | f3ead5b4624143a40b36889ad3a0651cb4a06890 /cmd/podman | |
parent | 4aeac1195a9463b52cf6c56791e9469e484346fb (diff) | |
parent | db3a4c070f76b8e2599e7428a054e4d072e36d85 (diff) | |
download | podman-1422cdbac192c7c9c907f88f11969c028d1e9611.tar.gz podman-1422cdbac192c7c9c907f88f11969c028d1e9611.tar.bz2 podman-1422cdbac192c7c9c907f88f11969c028d1e9611.zip |
Merge pull request #12460 from jwhonce/issues/10974-1
[NO NEW TESTS NEEDED] Refactor podman container command output
Diffstat (limited to 'cmd/podman')
-rw-r--r-- | cmd/podman/containers/mount.go | 14 | ||||
-rw-r--r-- | cmd/podman/containers/ps.go | 24 | ||||
-rw-r--r-- | cmd/podman/containers/stats.go | 33 | ||||
-rw-r--r-- | cmd/podman/containers/top.go | 12 |
4 files changed, 35 insertions, 48 deletions
diff --git a/cmd/podman/containers/mount.go b/cmd/podman/containers/mount.go index 0397b456f..271fc4c6b 100644 --- a/cmd/podman/containers/mount.go +++ b/cmd/podman/containers/mount.go @@ -81,7 +81,7 @@ func init() { validate.AddLatestFlag(containerMountCommand, &mountOpts.Latest) } -func mount(_ *cobra.Command, args []string) error { +func mount(cmd *cobra.Command, args []string) error { if len(args) > 0 && mountOpts.Latest { return errors.Errorf("--latest and containers cannot be used together") } @@ -116,18 +116,14 @@ func mount(_ *cobra.Command, args []string) error { mrs = append(mrs, mountReporter{r}) } - format := "{{range . }}{{.ID}}\t{{.Path}}\n{{end -}}" - tmpl, err := report.NewTemplate("mounts").Parse(format) - if err != nil { - return err - } + rpt := report.New(os.Stdout, cmd.Name()) + defer rpt.Flush() - w, err := report.NewWriterDefault(os.Stdout) + rpt, err = rpt.Parse(report.OriginPodman, "{{range . }}{{.ID}}\t{{.Path}}\n{{end -}}") if err != nil { return err } - defer w.Flush() - return tmpl.Execute(w, mrs) + return rpt.Execute(mrs) } func printJSON(reports []*entities.ContainerMountReport) error { diff --git a/cmd/podman/containers/ps.go b/cmd/podman/containers/ps.go index 712de327c..cebe61b5a 100644 --- a/cmd/podman/containers/ps.go +++ b/cmd/podman/containers/ps.go @@ -220,30 +220,28 @@ func ps(cmd *cobra.Command, _ []string) error { hdrs, format := createPsOut() + var origin report.Origin noHeading, _ := cmd.Flags().GetBool("noheading") if cmd.Flags().Changed("format") { noHeading = noHeading || !report.HasTable(listOpts.Format) - format = report.NormalizeFormat(listOpts.Format) - format = report.EnforceRange(format) + format = listOpts.Format + origin = report.OriginUser + } else { + origin = report.OriginPodman } ns := strings.NewReplacer(".Namespaces.", ".") format = ns.Replace(format) - tmpl, err := report.NewTemplate("list").Parse(format) - if err != nil { - return err - } - - w, err := report.NewWriterDefault(os.Stdout) + rpt, err := report.New(os.Stdout, cmd.Name()).Parse(origin, format) if err != nil { return err } - defer w.Flush() + defer rpt.Flush() headers := func() error { return nil } if !noHeading { headers = func() error { - return tmpl.Execute(w, hdrs) + return rpt.Execute(hdrs) } } @@ -268,10 +266,10 @@ func ps(cmd *cobra.Command, _ []string) error { if err := headers(); err != nil { return err } - if err := tmpl.Execute(w, responses); err != nil { + if err := rpt.Execute(responses); err != nil { return err } - if err := w.Flush(); err != nil { + if err := rpt.Flush(); err != nil { // we usually do not care about Flush() failures but here do not loop if Flush() has failed return err } @@ -282,7 +280,7 @@ func ps(cmd *cobra.Command, _ []string) error { if err := headers(); err != nil { return err } - if err := tmpl.Execute(w, responses); err != nil { + if err := rpt.Execute(responses); err != nil { return err } } diff --git a/cmd/podman/containers/stats.go b/cmd/podman/containers/stats.go index d21feaabc..9fba51597 100644 --- a/cmd/podman/containers/stats.go +++ b/cmd/podman/containers/stats.go @@ -126,14 +126,14 @@ func stats(cmd *cobra.Command, args []string) error { if report.Error != nil { return report.Error } - if err := outputStats(report.Stats); err != nil { + if err := outputStats(cmd, report.Stats); err != nil { return err } } return nil } -func outputStats(reports []define.ContainerStats) error { +func outputStats(cmd *cobra.Command, reports []define.ContainerStats) error { headers := report.Headers(define.ContainerStats{}, map[string]string{ "ID": "ID", "UpTime": "CPU TIME", @@ -158,32 +158,27 @@ func outputStats(reports []define.ContainerStats) error { if report.IsJSON(statsOptions.Format) { return outputJSON(stats) } - format := "{{.ID}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDS}}\t{{.UpTime}}\t{{.AVGCPU}}\n" - if len(statsOptions.Format) > 0 { - format = report.NormalizeFormat(statsOptions.Format) - } - format = report.EnforceRange(format) - tmpl, err := report.NewTemplate("stats").Parse(format) - if err != nil { - return err - } + rpt := report.New(os.Stdout, cmd.Name()) + defer rpt.Flush() - w, err := report.NewWriterDefault(os.Stdout) + var err error + if cmd.Flags().Changed("format") { + rpt, err = rpt.Parse(report.OriginUser, statsOptions.Format) + } else { + format := "{{range .}}{{.ID}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDS}}\t{{.UpTime}}\t{{.AVGCPU}}\n{{end -}}" + rpt, err = rpt.Parse(report.OriginPodman, format) + } if err != nil { return err } - defer w.Flush() - if len(statsOptions.Format) < 1 { - if err := tmpl.Execute(w, headers); err != nil { + if rpt.RenderHeaders { + if err := rpt.Execute(headers); err != nil { return err } } - if err := tmpl.Execute(w, stats); err != nil { - return err - } - return nil + return rpt.Execute(stats) } type containerStats struct { diff --git a/cmd/podman/containers/top.go b/cmd/podman/containers/top.go index 808c6c494..963a251bb 100644 --- a/cmd/podman/containers/top.go +++ b/cmd/podman/containers/top.go @@ -77,7 +77,7 @@ func init() { validate.AddLatestFlag(containerTopCommand, &topOptions.Latest) } -func top(_ *cobra.Command, args []string) error { +func top(cmd *cobra.Command, args []string) error { if topOptions.ListDescriptors { descriptors, err := util.GetContainerPidInformationDescriptors() if err != nil { @@ -103,15 +103,13 @@ func top(_ *cobra.Command, args []string) error { return err } - w, err := report.NewWriterDefault(os.Stdout) - if err != nil { - return err - } + rpt := report.New(os.Stdout, cmd.Name()).Init(os.Stdout, 12, 2, 2, ' ', 0) + defer rpt.Flush() for _, proc := range topResponse.Value { - if _, err := fmt.Fprintln(w, proc); err != nil { + if _, err := fmt.Fprintln(rpt.Writer(), proc); err != nil { return err } } - return w.Flush() + return nil } |