Skip to content

Commit

Permalink
podman machine 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.

Also fix a bug where a invlaid template would not cause a exit code > 0,
see the added test case.

[1] containers/common#1146

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Sep 7, 2022
1 parent 833db20 commit ad574fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
20 changes: 7 additions & 13 deletions cmd/podman/machine/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/cmd/podman/utils"
"github.com/containers/podman/v4/pkg/machine"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -66,28 +65,23 @@ func inspect(cmd *cobra.Command, args []string) error {
}
vms = append(vms, *ii)
}

switch {
case cmd.Flag("format").Changed:
row := report.NormalizeFormat(inspectFlag.format)
row = report.EnforceRange(row)
rpt := report.New(os.Stdout, cmd.Name())
defer rpt.Flush()

tmpl, err := report.NewTemplate("Machine inspect").Parse(row)
rpt, err := rpt.Parse(report.OriginUser, inspectFlag.format)
if err != nil {
return err
}

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

if err := tmpl.Execute(w, vms); err != nil {
logrus.Error(err)
if err := rpt.Execute(vms); err != nil {
errs = append(errs, err)
}
w.Flush()
default:
if err := printJSON(vms); err != nil {
logrus.Error(err)
errs = append(errs, err)
}
}
return errs.PrintErrors()
Expand Down
8 changes: 8 additions & 0 deletions pkg/machine/e2e/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,13 @@ var _ = Describe("podman machine stop", func() {
Expect(err).To(BeNil())
Expect(inspectSession).To(Exit(0))
Expect(inspectSession.Bytes()).To(ContainSubstring(name))

// check invalid template returns error
inspect = new(inspectMachine)
inspect = inspect.withFormat("{{.Abcde}}")
inspectSession, err = mb.setName(name).setCmd(inspect).run()
Expect(err).To(BeNil())
Expect(inspectSession).To(Exit(125))
Expect(inspectSession.errorToString()).To(ContainSubstring("can't evaluate field Abcde in type machine.InspectInfo"))
})
})

0 comments on commit ad574fc

Please sign in to comment.