Skip to content

Commit

Permalink
podman inspect: 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 7, 2022
1 parent 438403b commit d130bff
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions cmd/podman/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"regexp"
"strings"
"text/template"

"github.com/containers/common/pkg/completion"
"github.com/containers/common/pkg/report"
Expand Down Expand Up @@ -201,10 +200,14 @@ func (i *inspector) inspect(namesOrIDs []string) error {
err = printJSON(data)
default:
// Landing here implies user has given a custom --format
row := inspectNormalize(i.options.Format, tmpType)
row = report.NormalizeFormat(row)
row = report.EnforceRange(row)
err = printTmpl(tmpType, row, data)
var rpt *report.Formatter
rpt, err = report.New(os.Stdout, "inspect").Parse(report.OriginUser, i.options.Format)
if err != nil {
return err
}
defer rpt.Flush()

err = rpt.Execute(data)
}
if err != nil {
logrus.Errorf("Printing inspect output: %v", err)
Expand All @@ -230,22 +233,6 @@ func printJSON(data []interface{}) error {
return enc.Encode(data)
}

func printTmpl(typ, row string, data []interface{}) error {
// We cannot use c/common/reports here, too many levels of interface{}
t, err := template.New(typ + " inspect").Funcs(template.FuncMap(report.DefaultFuncs)).Parse(row)
if err != nil {
return err
}

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

func (i *inspector) inspectAll(ctx context.Context, namesOrIDs []string) ([]interface{}, []error, error) {
var data []interface{}
allErrs := []error{}
Expand Down

0 comments on commit d130bff

Please sign in to comment.