Skip to content

Commit

Permalink
podman network ls: use report.Formatter over Template
Browse files Browse the repository at this point in the history
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] containers/common#1146

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Sep 7, 2022
1 parent 4e82ebc commit 5e06d75
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions cmd/podman/networks/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5e06d75

Please sign in to comment.