mirror of
https://github.com/containers/podman.git
synced 2025-09-23 13:13:42 +08:00
[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>
This commit is contained in:
@ -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 {
|
||||
|
@ -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)
|
||||
rpt, err := report.New(os.Stdout, cmd.Name()).Parse(origin, format)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w, err := report.NewWriterDefault(os.Stdout)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
rpt := report.New(os.Stdout, cmd.Name())
|
||||
defer rpt.Flush()
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
w, err := report.NewWriterDefault(os.Stdout)
|
||||
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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user