Skip to content

Commit

Permalink
podman secret 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 13, 2022
1 parent 1463898 commit f5e13de
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions cmd/podman/secrets/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
flags := lsCmd.Flags()

formatFlagName := "format"
flags.StringVar(&listFlag.format, formatFlagName, "{{.ID}}\t{{.Name}}\t{{.Driver}}\t{{.CreatedAt}}\t{{.UpdatedAt}}\t\n", "Format volume output using Go template")
flags.StringVar(&listFlag.format, formatFlagName, "{{range .}}{{.ID}}\t{{.Name}}\t{{.Driver}}\t{{.CreatedAt}}\t{{.UpdatedAt}}\n{{end -}}", "Format volume output using Go template")
_ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&entities.SecretInfoReport{}))

filterFlagName := "filter"
Expand Down Expand Up @@ -105,31 +105,25 @@ func outputTemplate(cmd *cobra.Command, responses []*entities.SecretListReport)
"UpdatedAt": "UPDATED",
})

row := cmd.Flag("format").Value.String()
if cmd.Flags().Changed("format") {
row = report.NormalizeFormat(row)
}
format := report.EnforceRange(row)
rpt := report.New(os.Stdout, cmd.Name())
defer rpt.Flush()

tmpl, err := report.NewTemplate("list").Parse(format)
if err != nil {
return err
var err error
switch {
case cmd.Flag("format").Changed:
rpt, err = rpt.Parse(report.OriginUser, listFlag.format)
default:
rpt, err = rpt.Parse(report.OriginPodman, listFlag.format)
}

w, err := report.NewWriterDefault(os.Stdout)
if err != nil {
return err
}
defer w.Flush()

if cmd.Flags().Changed("format") && !report.HasTable(listFlag.format) {
listFlag.noHeading = true
}

if !listFlag.noHeading {
if err := tmpl.Execute(w, headers); err != nil {
noHeading, _ := cmd.Flags().GetBool("noheading")
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)
}

0 comments on commit f5e13de

Please sign in to comment.