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)
|
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 {
|
if len(args) > 0 && mountOpts.Latest {
|
||||||
return errors.Errorf("--latest and containers cannot be used together")
|
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})
|
mrs = append(mrs, mountReporter{r})
|
||||||
}
|
}
|
||||||
|
|
||||||
format := "{{range . }}{{.ID}}\t{{.Path}}\n{{end -}}"
|
rpt := report.New(os.Stdout, cmd.Name())
|
||||||
tmpl, err := report.NewTemplate("mounts").Parse(format)
|
defer rpt.Flush()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
w, err := report.NewWriterDefault(os.Stdout)
|
rpt, err = rpt.Parse(report.OriginPodman, "{{range . }}{{.ID}}\t{{.Path}}\n{{end -}}")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer w.Flush()
|
return rpt.Execute(mrs)
|
||||||
return tmpl.Execute(w, mrs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func printJSON(reports []*entities.ContainerMountReport) error {
|
func printJSON(reports []*entities.ContainerMountReport) error {
|
||||||
|
@ -220,30 +220,28 @@ func ps(cmd *cobra.Command, _ []string) error {
|
|||||||
|
|
||||||
hdrs, format := createPsOut()
|
hdrs, format := createPsOut()
|
||||||
|
|
||||||
|
var origin report.Origin
|
||||||
noHeading, _ := cmd.Flags().GetBool("noheading")
|
noHeading, _ := cmd.Flags().GetBool("noheading")
|
||||||
if cmd.Flags().Changed("format") {
|
if cmd.Flags().Changed("format") {
|
||||||
noHeading = noHeading || !report.HasTable(listOpts.Format)
|
noHeading = noHeading || !report.HasTable(listOpts.Format)
|
||||||
format = report.NormalizeFormat(listOpts.Format)
|
format = listOpts.Format
|
||||||
format = report.EnforceRange(format)
|
origin = report.OriginUser
|
||||||
|
} else {
|
||||||
|
origin = report.OriginPodman
|
||||||
}
|
}
|
||||||
ns := strings.NewReplacer(".Namespaces.", ".")
|
ns := strings.NewReplacer(".Namespaces.", ".")
|
||||||
format = ns.Replace(format)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer rpt.Flush()
|
||||||
w, err := report.NewWriterDefault(os.Stdout)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer w.Flush()
|
|
||||||
|
|
||||||
headers := func() error { return nil }
|
headers := func() error { return nil }
|
||||||
if !noHeading {
|
if !noHeading {
|
||||||
headers = func() error {
|
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 {
|
if err := headers(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := tmpl.Execute(w, responses); err != nil {
|
if err := rpt.Execute(responses); err != nil {
|
||||||
return err
|
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
|
// we usually do not care about Flush() failures but here do not loop if Flush() has failed
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -282,7 +280,7 @@ func ps(cmd *cobra.Command, _ []string) error {
|
|||||||
if err := headers(); err != nil {
|
if err := headers(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := tmpl.Execute(w, responses); err != nil {
|
if err := rpt.Execute(responses); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,14 +126,14 @@ func stats(cmd *cobra.Command, args []string) error {
|
|||||||
if report.Error != nil {
|
if report.Error != nil {
|
||||||
return report.Error
|
return report.Error
|
||||||
}
|
}
|
||||||
if err := outputStats(report.Stats); err != nil {
|
if err := outputStats(cmd, report.Stats); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
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{
|
headers := report.Headers(define.ContainerStats{}, map[string]string{
|
||||||
"ID": "ID",
|
"ID": "ID",
|
||||||
"UpTime": "CPU TIME",
|
"UpTime": "CPU TIME",
|
||||||
@ -158,32 +158,27 @@ func outputStats(reports []define.ContainerStats) error {
|
|||||||
if report.IsJSON(statsOptions.Format) {
|
if report.IsJSON(statsOptions.Format) {
|
||||||
return outputJSON(stats)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
w, err := report.NewWriterDefault(os.Stdout)
|
if rpt.RenderHeaders {
|
||||||
if err != nil {
|
if err := rpt.Execute(headers); err != nil {
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer w.Flush()
|
|
||||||
|
|
||||||
if len(statsOptions.Format) < 1 {
|
|
||||||
if err := tmpl.Execute(w, headers); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := tmpl.Execute(w, stats); err != nil {
|
return rpt.Execute(stats)
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type containerStats struct {
|
type containerStats struct {
|
||||||
|
@ -77,7 +77,7 @@ func init() {
|
|||||||
validate.AddLatestFlag(containerTopCommand, &topOptions.Latest)
|
validate.AddLatestFlag(containerTopCommand, &topOptions.Latest)
|
||||||
}
|
}
|
||||||
|
|
||||||
func top(_ *cobra.Command, args []string) error {
|
func top(cmd *cobra.Command, args []string) error {
|
||||||
if topOptions.ListDescriptors {
|
if topOptions.ListDescriptors {
|
||||||
descriptors, err := util.GetContainerPidInformationDescriptors()
|
descriptors, err := util.GetContainerPidInformationDescriptors()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -103,15 +103,13 @@ func top(_ *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
w, err := report.NewWriterDefault(os.Stdout)
|
rpt := report.New(os.Stdout, cmd.Name()).Init(os.Stdout, 12, 2, 2, ' ', 0)
|
||||||
if err != nil {
|
defer rpt.Flush()
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, proc := range topResponse.Value {
|
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 err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return w.Flush()
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user