diff options
author | Jhon Honce <jhonce@redhat.com> | 2021-12-02 11:45:48 -0700 |
---|---|---|
committer | Jhon Honce <jhonce@redhat.com> | 2021-12-06 10:51:29 -0700 |
commit | d35aa3f293c2132bc32de9cf4dcffe8b5bf6f8c8 (patch) | |
tree | 4bdee5d83e007cf0d3cf636397e1aae966948626 /cmd/podman/pods/stats.go | |
parent | 6d1b2dced4e8e8651007fd245331b0ab377f1b33 (diff) | |
download | podman-d35aa3f293c2132bc32de9cf4dcffe8b5bf6f8c8.tar.gz podman-d35aa3f293c2132bc32de9cf4dcffe8b5bf6f8c8.tar.bz2 podman-d35aa3f293c2132bc32de9cf4dcffe8b5bf6f8c8.zip |
Refactor podman pods to report.Formatter
[NO NEW TESTS NEEDED]
Support better compatibility output for podman pods commands
See #10974
Depends on containers/common#831
Signed-off-by: Jhon Honce <jhonce@redhat.com>
Diffstat (limited to 'cmd/podman/pods/stats.go')
-rw-r--r-- | cmd/podman/pods/stats.go | 81 |
1 files changed, 40 insertions, 41 deletions
diff --git a/cmd/podman/pods/stats.go b/cmd/podman/pods/stats.go index a7bba3064..fbb9e0b52 100644 --- a/cmd/podman/pods/stats.go +++ b/cmd/podman/pods/stats.go @@ -67,15 +67,22 @@ func stats(cmd *cobra.Command, args []string) error { return err } - doJSON := report.IsJSON(cmd.Flag("format").Value.String()) - headers := report.Headers(entities.PodStatsReport{}, map[string]string{ - "CPU": "CPU %", - "MemUsage": "MEM USAGE/ LIMIT", - "MemUsageBytes": "MEM USAGE/ LIMIT", - "MEM": "MEM %", - "NET IO": "NET IO", - "BlockIO": "BLOCK IO", - }) + rpt := report.New(os.Stdout, cmd.Name()) + defer rpt.Flush() + + var err error + doJSON := report.IsJSON(statsOptions.Format) + if !doJSON { + if cmd.Flags().Changed("format") { + rpt, err = rpt.Parse(report.OriginUser, statsOptions.Format) + if err != nil { + return err + } + } else { + rpt = rpt.Init(os.Stdout, 12, 2, 2, ' ', 0) + rpt.Origin = report.OriginPodman + } + } for ; ; time.Sleep(time.Second) { reports, err := registry.ContainerEngine().PodStats(context.Background(), args, statsOptions.PodStatsOptions) @@ -84,30 +91,26 @@ func stats(cmd *cobra.Command, args []string) error { } // Print the stats in the requested format and configuration. if doJSON { - if err := printJSONPodStats(reports); err != nil { - return err - } + err = printJSONPodStats(reports) } else { if !statsOptions.NoReset { goterm.Clear() goterm.MoveCursor(1, 1) goterm.Flush() } - if cmd.Flags().Changed("format") { - row := report.NormalizeFormat(statsOptions.Format) - row = report.EnforceRange(row) - if err := printFormattedPodStatsLines(headers, row, reports); err != nil { - return err - } + if report.OriginUser == rpt.Origin { + err = userTemplate(rpt, reports) } else { - printPodStatsLines(reports) + err = defaultTemplate(rpt, reports) } } + if err != nil { + return err + } if statsOptions.NoStream { break } } - return nil } @@ -120,42 +123,38 @@ func printJSONPodStats(stats []*entities.PodStatsReport) error { return nil } -func printPodStatsLines(stats []*entities.PodStatsReport) error { - w, err := report.NewWriterDefault(os.Stdout) - if err != nil { - return err - } - +func defaultTemplate(rpt *report.Formatter, stats []*entities.PodStatsReport) error { outFormat := "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" - fmt.Fprintf(w, outFormat, "POD", "CID", "NAME", "CPU %", "MEM USAGE/ LIMIT", "MEM %", "NET IO", "BLOCK IO", "PIDS") + fmt.Fprintf(rpt.Writer(), outFormat, "POD", "CID", "NAME", "CPU %", "MEM USAGE/ LIMIT", "MEM %", "NET IO", "BLOCK IO", "PIDS") if len(stats) == 0 { - fmt.Fprintf(w, outFormat, "--", "--", "--", "--", "--", "--", "--", "--", "--") + fmt.Fprintf(rpt.Writer(), outFormat, "--", "--", "--", "--", "--", "--", "--", "--", "--") } else { for _, i := range stats { - fmt.Fprintf(w, outFormat, i.Pod, i.CID, i.Name, i.CPU, i.MemUsage, i.Mem, i.NetIO, i.BlockIO, i.PIDS) + fmt.Fprintf(rpt.Writer(), outFormat, i.Pod, i.CID, i.Name, i.CPU, i.MemUsage, i.Mem, i.NetIO, i.BlockIO, i.PIDS) } } - return w.Flush() + return rpt.Flush() } -func printFormattedPodStatsLines(headerNames []map[string]string, row string, stats []*entities.PodStatsReport) error { +func userTemplate(rpt *report.Formatter, stats []*entities.PodStatsReport) error { if len(stats) == 0 { return nil } - tmpl, err := report.NewTemplate("stats").Parse(row) - if err != nil { - return err - } + headers := report.Headers(entities.PodStatsReport{}, map[string]string{ + "CPU": "CPU %", + "MemUsage": "MEM USAGE/ LIMIT", + "MemUsageBytes": "MEM USAGE/ LIMIT", + "MEM": "MEM %", + "NET IO": "NET IO", + "BlockIO": "BLOCK IO", + }) - w, err := report.NewWriterDefault(os.Stdout) - if err != nil { + if err := rpt.Execute(headers); err != nil { return err } - defer w.Flush() - - if err := tmpl.Execute(w, headerNames); err != nil { + if err := rpt.Execute(stats); err != nil { return err } - return tmpl.Execute(w, stats) + return rpt.Flush() } |