diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/podman/machine/inspect.go | 31 | ||||
-rw-r--r-- | cmd/podman/machine/machine.go | 32 |
2 files changed, 28 insertions, 35 deletions
diff --git a/cmd/podman/machine/inspect.go b/cmd/podman/machine/inspect.go index 0ddcec8b5..b8de8e4f4 100644 --- a/cmd/podman/machine/inspect.go +++ b/cmd/podman/machine/inspect.go @@ -6,10 +6,10 @@ package machine import ( "os" + "github.com/containers/common/pkg/report" "github.com/containers/podman/v4/cmd/podman/common" "github.com/containers/podman/v4/cmd/podman/registry" "github.com/containers/podman/v4/cmd/podman/utils" - "github.com/containers/podman/v4/libpod/define" "github.com/containers/podman/v4/pkg/machine" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -65,12 +65,29 @@ func inspect(cmd *cobra.Command, args []string) error { } vms = append(vms, *ii) } - if len(inspectFlag.format) > 0 { - // need jhonce to work his template magic - return define.ErrNotImplemented - } - if err := printJSON(vms); err != nil { - logrus.Error(err) + switch { + case cmd.Flag("format").Changed: + row := report.NormalizeFormat(inspectFlag.format) + row = report.EnforceRange(row) + + tmpl, err := report.NewTemplate("Machine inspect").Parse(row) + 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) + } + w.Flush() + default: + if err := printJSON(vms); err != nil { + logrus.Error(err) + } } return errs.PrintErrors() } diff --git a/cmd/podman/machine/machine.go b/cmd/podman/machine/machine.go index 4c566b11f..cf7542389 100644 --- a/cmd/podman/machine/machine.go +++ b/cmd/podman/machine/machine.go @@ -26,7 +26,6 @@ var ( // Pull in configured json library json = registry.JSONLibrary() - sockPaths []string // Paths to unix domain sockets for publishing openEventSock sync.Once // Singleton support for opening sockets as needed sockets []net.Conn // Opened sockets, if any @@ -35,7 +34,7 @@ var ( Use: "machine", Short: "Manage a virtual machine", Long: "Manage a virtual machine. Virtual machines are used to run Podman.", - PersistentPreRunE: initMachineEvents, + PersistentPreRunE: validate.NoOp, PersistentPostRunE: closeMachineEvents, RunE: validate.SubCommandExists, } @@ -79,17 +78,10 @@ func getMachines(toComplete string) ([]string, cobra.ShellCompDirective) { return suggestions, cobra.ShellCompDirectiveNoFileComp } -func initMachineEvents(cmd *cobra.Command, _ []string) error { - logrus.Debugf("Called machine %s.PersistentPreRunE(%s)", cmd.Name(), strings.Join(os.Args, " ")) - +func initMachineEvents() { sockPaths, err := resolveEventSock() if err != nil { - return err - } - - // No sockets found, so no need to publish events... - if len(sockPaths) == 0 { - return nil + logrus.Warnf("Failed to resolve machine event sockets, machine events will not be published: %v", err) } for _, path := range sockPaths { @@ -101,7 +93,6 @@ func initMachineEvents(cmd *cobra.Command, _ []string) error { logrus.Debugf("Machine event socket %q found", path) sockets = append(sockets, conn) } - return nil } func resolveEventSock() ([]string, error) { @@ -145,22 +136,7 @@ func resolveEventSock() ([]string, error) { } func newMachineEvent(status events.Status, event events.Event) { - openEventSock.Do(func() { - // No sockets where found, so no need to publish events... - if len(sockPaths) == 0 { - return - } - - for _, path := range sockPaths { - conn, err := (&net.Dialer{}).DialContext(registry.Context(), "unix", path) - if err != nil { - logrus.Warnf("Failed to open event socket %q: %v", path, err) - continue - } - logrus.Debugf("Machine event socket %q found", path) - sockets = append(sockets, conn) - } - }) + openEventSock.Do(initMachineEvents) event.Status = status event.Time = time.Now() |