summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJhon Honce <jhonce@redhat.com>2021-11-30 15:34:58 -0700
committerJhon Honce <jhonce@redhat.com>2021-12-01 11:17:40 -0700
commitdb3a4c070f76b8e2599e7428a054e4d072e36d85 (patch)
tree570e3f406c7d771a7f6f147d82be34986a27d344
parent295a6f7dd086731448a1168a349f62d3035258ca (diff)
downloadpodman-db3a4c070f76b8e2599e7428a054e4d072e36d85.tar.gz
podman-db3a4c070f76b8e2599e7428a054e4d072e36d85.tar.bz2
podman-db3a4c070f76b8e2599e7428a054e4d072e36d85.zip
[NO NEW TESTS NEEDED] Refactor podman container command output
Leverage new report.Formatter allowing better compatibility from podman command output. See #10974 See #12455 Depends on containers/common#831 Signed-off-by: Jhon Honce <jhonce@redhat.com>
-rw-r--r--cmd/podman/containers/mount.go14
-rw-r--r--cmd/podman/containers/ps.go24
-rw-r--r--cmd/podman/containers/stats.go33
-rw-r--r--cmd/podman/containers/top.go12
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
}