Skip to content

Commit

Permalink
podman machine 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 65e78d9 commit a687949
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions cmd/podman/machine/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func init() {

flags := lsCmd.Flags()
formatFlagName := "format"
flags.StringVar(&listFlag.format, formatFlagName, "{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n", "Format volume output using JSON or a Go template")
flags.StringVar(&listFlag.format, formatFlagName, "{{range .}}{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n{{end -}}", "Format volume output using JSON or a Go template")
_ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&entities.ListReporter{}))
flags.BoolVar(&listFlag.noHeading, "noheading", false, "Do not print headers")
flags.BoolVarP(&listFlag.quiet, "quiet", "q", false, "Show only machine names")
Expand All @@ -66,10 +66,6 @@ func list(cmd *cobra.Command, args []string) error {
err error
)

if listFlag.quiet {
listFlag.format = "{{.Name}}\n"
}

provider := GetSystemDefaultProvider()
listResponse, err = provider.List(opts)
if err != nil {
Expand Down Expand Up @@ -115,37 +111,29 @@ func outputTemplate(cmd *cobra.Command, responses []*entities.ListReporter) erro
"Memory": "MEMORY",
"DiskSize": "DISK SIZE",
})
printHeader := !listFlag.noHeading
if listFlag.quiet {
printHeader = false
}
var row string

rpt := report.New(os.Stdout, cmd.Name())
defer rpt.Flush()

var err error
switch {
case cmd.Flags().Changed("format"):
row = cmd.Flag("format").Value.String()
printHeader = report.HasTable(row)
row = report.NormalizeFormat(row)
case cmd.Flag("format").Changed:
rpt, err = rpt.Parse(report.OriginUser, listFlag.format)
case listFlag.quiet:
rpt, err = rpt.Parse(report.OriginUser, "{{.Name}}\n")
default:
row = cmd.Flag("format").Value.String()
rpt, err = rpt.Parse(report.OriginPodman, listFlag.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 printHeader {
if err := tmpl.Execute(w, headers); err != nil {
if rpt.RenderHeaders && !listFlag.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 strTime(t time.Time) string {
Expand Down

0 comments on commit a687949

Please sign in to comment.