From 1463898b070eddb20f15ef12d7cb2133f35e431c Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Wed, 7 Sep 2022 14:26:06 +0200 Subject: podman network ls: use report.Formatter over Template Currently the podman command --format output code uses a mix of report.Formatter and report.Template. I patched report.Formatter to correctly handle newlines[1]. Since we cannot fix this with report.Template we have to migrate all users to report.Formatter. This ensures consistent behavior for all commands. This change does not change the output, we can add a new test for the newline bug when the common PR is vendored in. [1] https://github.com/containers/common/pull/1146 Signed-off-by: Paul Holzinger --- cmd/podman/networks/list.go | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) (limited to 'cmd/podman') diff --git a/cmd/podman/networks/list.go b/cmd/podman/networks/list.go index 5d4be2a81..951583cfb 100644 --- a/cmd/podman/networks/list.go +++ b/cmd/podman/networks/list.go @@ -122,36 +122,27 @@ func templateOut(cmd *cobra.Command, responses []types.Network) error { "ID": "network id", }) - renderHeaders := report.HasTable(networkListOptions.Format) - var row string + rpt := report.New(os.Stdout, cmd.Name()) + defer rpt.Flush() + + var err error switch { - case cmd.Flags().Changed("format"): - row = report.NormalizeFormat(networkListOptions.Format) + case cmd.Flag("format").Changed: + rpt, err = rpt.Parse(report.OriginUser, networkListOptions.Format) default: - // 'podman network ls' equivalent to 'podman network ls --format="table {{.ID}} {{.Name}} {{.Version}} {{.Plugins}}" ' - row = "{{.ID}}\t{{.Name}}\t{{.Driver}}\n" - renderHeaders = true + rpt, err = rpt.Parse(report.OriginPodman, "{{range .}}{{.ID}}\t{{.Name}}\t{{.Driver}}\n{{end -}}") } - format := report.EnforceRange(row) - - tmpl, err := report.NewTemplate("list").Parse(format) - if err != nil { - return err - } - - w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - defer w.Flush() noHeading, _ := cmd.Flags().GetBool("noheading") - if !noHeading && renderHeaders { - if err := tmpl.Execute(w, headers); err != nil { - return err + if rpt.RenderHeaders && !noHeading { + if err := rpt.Execute(headers); err != nil { + return fmt.Errorf("failed to write report column headers: %w", err) } } - return tmpl.Execute(w, nlprs) + return rpt.Execute(nlprs) } // ListPrintReports returns the network list report -- cgit v1.2.3-54-g00ecf