Skip to content

Commit

Permalink
[NO NEW TESTS NEEDED] Refactor podman container command output
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jwhonce committed Dec 1, 2021
1 parent 295a6f7 commit db3a4c0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 48 deletions.
14 changes: 5 additions & 9 deletions cmd/podman/containers/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 11 additions & 13 deletions cmd/podman/containers/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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
}
Expand All @@ -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
}
}
Expand Down
33 changes: 14 additions & 19 deletions cmd/podman/containers/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions cmd/podman/containers/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit db3a4c0

Please sign in to comment.