podman version: 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.

[1] https://github.com/containers/common/pull/1146

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2022-09-08 14:02:40 +02:00
parent 0c21dcf70c
commit 7f8e99ded4
No known key found for this signature in database
GPG Key ID: EB145DD938A3CAF2
2 changed files with 11 additions and 10 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"os" "os"
"strings" "strings"
"text/template"
"github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/completion"
"github.com/containers/common/pkg/report" "github.com/containers/common/pkg/report"
@ -12,6 +11,7 @@ import (
"github.com/containers/podman/v4/cmd/podman/registry" "github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/cmd/podman/validate" "github.com/containers/podman/v4/cmd/podman/validate"
"github.com/containers/podman/v4/pkg/domain/entities" "github.com/containers/podman/v4/pkg/domain/entities"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -53,22 +53,25 @@ func version(cmd *cobra.Command, args []string) error {
} }
if cmd.Flag("format").Changed { if cmd.Flag("format").Changed {
// Cannot use report.New() as it enforces {{range .}} for OriginUser templates rpt := report.New(os.Stdout, cmd.Name())
tmpl := template.New(cmd.Name()).Funcs(template.FuncMap(report.DefaultFuncs)) defer rpt.Flush()
versionFormat = report.NormalizeFormat(versionFormat) // Use OriginUnknown so it does not add an extra range since it
tmpl, err := tmpl.Parse(versionFormat) // will only be called for a single element and not a slice.
rpt, err = rpt.Parse(report.OriginUnknown, versionFormat)
if err != nil { if err != nil {
return err return err
} }
if err := tmpl.Execute(os.Stdout, versions); err != nil { if err := rpt.Execute(versions); err != nil {
// only log at debug since we fall back to the client only template
logrus.Debugf("Failed to execute template: %v", err)
// On Failure, assume user is using older version of podman version --format and check client // On Failure, assume user is using older version of podman version --format and check client
versionFormat = strings.ReplaceAll(versionFormat, ".Server.", ".") versionFormat = strings.ReplaceAll(versionFormat, ".Server.", ".")
tmpl, err := tmpl.Parse(versionFormat) rpt, err := rpt.Parse(report.OriginUnknown, versionFormat)
if err != nil { if err != nil {
return err return err
} }
if err := tmpl.Execute(os.Stdout, versions.Client); err != nil { if err := rpt.Execute(versions.Client); err != nil {
return err return err
} }
} }

View File

@ -20,8 +20,6 @@ function teardown() {
# remove the entire lines, except for pod-inspect, just remove the SKIP # remove the entire lines, except for pod-inspect, just remove the SKIP
# but leave "mypod") # but leave "mypod")
extra_args_table=" extra_args_table="
version | SKIP
history | $IMAGE history | $IMAGE
image history | $IMAGE image history | $IMAGE
image inspect | $IMAGE image inspect | $IMAGE