mirror of
https://github.com/containers/podman.git
synced 2025-06-23 02:18:13 +08:00
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 <pholzing@redhat.com>
This commit is contained in:
@ -122,36 +122,27 @@ func templateOut(cmd *cobra.Command, responses []types.Network) error {
|
|||||||
"ID": "network id",
|
"ID": "network id",
|
||||||
})
|
})
|
||||||
|
|
||||||
renderHeaders := report.HasTable(networkListOptions.Format)
|
rpt := report.New(os.Stdout, cmd.Name())
|
||||||
var row string
|
defer rpt.Flush()
|
||||||
|
|
||||||
|
var err error
|
||||||
switch {
|
switch {
|
||||||
case cmd.Flags().Changed("format"):
|
case cmd.Flag("format").Changed:
|
||||||
row = report.NormalizeFormat(networkListOptions.Format)
|
rpt, err = rpt.Parse(report.OriginUser, networkListOptions.Format)
|
||||||
default:
|
default:
|
||||||
// 'podman network ls' equivalent to 'podman network ls --format="table {{.ID}} {{.Name}} {{.Version}} {{.Plugins}}" '
|
rpt, err = rpt.Parse(report.OriginPodman, "{{range .}}{{.ID}}\t{{.Name}}\t{{.Driver}}\n{{end -}}")
|
||||||
row = "{{.ID}}\t{{.Name}}\t{{.Driver}}\n"
|
|
||||||
renderHeaders = true
|
|
||||||
}
|
}
|
||||||
format := report.EnforceRange(row)
|
|
||||||
|
|
||||||
tmpl, err := report.NewTemplate("list").Parse(format)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
w, err := report.NewWriterDefault(os.Stdout)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer w.Flush()
|
|
||||||
|
|
||||||
noHeading, _ := cmd.Flags().GetBool("noheading")
|
noHeading, _ := cmd.Flags().GetBool("noheading")
|
||||||
if !noHeading && renderHeaders {
|
if rpt.RenderHeaders && !noHeading {
|
||||||
if err := tmpl.Execute(w, headers); err != nil {
|
if err := rpt.Execute(headers); err != nil {
|
||||||
return err
|
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
|
// ListPrintReports returns the network list report
|
||||||
|
Reference in New Issue
Block a user