mirror of
https://github.com/containers/podman.git
synced 2025-06-23 02:18:13 +08:00
podman volume 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. Also fixa bug since the table format is expected to print headers as well. [1] https://github.com/containers/common/pull/1146 Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
@ -55,7 +55,7 @@ func init() {
|
||||
_ = lsCommand.RegisterFlagCompletionFunc(filterFlagName, common.AutocompleteVolumeFilters)
|
||||
|
||||
formatFlagName := "format"
|
||||
flags.StringVar(&cliOpts.Format, formatFlagName, "{{.Driver}}\t{{.Name}}\n", "Format volume output using Go template")
|
||||
flags.StringVar(&cliOpts.Format, formatFlagName, "{{range .}}{{.Driver}}\t{{.Name}}\n{{end -}}", "Format volume output using Go template")
|
||||
_ = lsCommand.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&entities.VolumeListReport{}))
|
||||
|
||||
flags.Bool("noheading", false, "Do not print headers")
|
||||
@ -95,34 +95,28 @@ func outputTemplate(cmd *cobra.Command, responses []*entities.VolumeListReport)
|
||||
"Name": "VOLUME NAME",
|
||||
})
|
||||
|
||||
var row string
|
||||
rpt := report.New(os.Stdout, cmd.Name())
|
||||
defer rpt.Flush()
|
||||
|
||||
var err error
|
||||
switch {
|
||||
case cmd.Flag("format").Changed:
|
||||
rpt, err = rpt.Parse(report.OriginUser, cliOpts.Format)
|
||||
case cliOpts.Quiet:
|
||||
row = "{{.Name}}\n"
|
||||
case cmd.Flags().Changed("format"):
|
||||
row = report.NormalizeFormat(cliOpts.Format)
|
||||
rpt, err = rpt.Parse(report.OriginUser, "{{.Name}}\n")
|
||||
default:
|
||||
row = cmd.Flag("format").Value.String()
|
||||
rpt, err = rpt.Parse(report.OriginPodman, cliOpts.Format)
|
||||
}
|
||||
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()
|
||||
|
||||
if !(noHeading || cliOpts.Quiet || cmd.Flag("format").Changed) {
|
||||
if err := tmpl.Execute(w, headers); err != nil {
|
||||
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, responses)
|
||||
return rpt.Execute(responses)
|
||||
}
|
||||
|
||||
func outputJSON(vols []*entities.VolumeListReport) error {
|
||||
|
@ -75,7 +75,10 @@ var _ = Describe("Podman volume ls", func() {
|
||||
session.WaitWithDefaultTimeout()
|
||||
|
||||
Expect(session).Should(Exit(0))
|
||||
Expect(session.OutputToStringArray()).To(HaveLen(1), session.OutputToString())
|
||||
arr := session.OutputToStringArray()
|
||||
Expect(arr).To(HaveLen(2))
|
||||
Expect(arr[0]).To(ContainSubstring("NAME"))
|
||||
Expect(arr[1]).To(ContainSubstring("myvol"))
|
||||
})
|
||||
|
||||
It("podman ls volume with --filter flag", func() {
|
||||
|
Reference in New Issue
Block a user