github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/pod_inspect.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/containers/libpod/cmd/podman/cliconfig"
     7  	"github.com/containers/libpod/pkg/adapter"
     8  	"github.com/pkg/errors"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var (
    13  	podInspectCommand     cliconfig.PodInspectValues
    14  	podInspectDescription = `Display the configuration for a pod by name or id
    15  
    16    By default, this will render all results in a JSON array.`
    17  
    18  	_podInspectCommand = &cobra.Command{
    19  		Use:   "inspect [flags] POD",
    20  		Short: "Displays a pod configuration",
    21  		Long:  podInspectDescription,
    22  		RunE: func(cmd *cobra.Command, args []string) error {
    23  			podInspectCommand.InputArgs = args
    24  			podInspectCommand.GlobalFlags = MainGlobalOpts
    25  			podInspectCommand.Remote = remoteclient
    26  			return podInspectCmd(&podInspectCommand)
    27  		},
    28  		Example: `podman pod inspect podID`,
    29  	}
    30  )
    31  
    32  func init() {
    33  	podInspectCommand.Command = _podInspectCommand
    34  	podInspectCommand.SetHelpTemplate(HelpTemplate())
    35  	podInspectCommand.SetUsageTemplate(UsageTemplate())
    36  	flags := podInspectCommand.Flags()
    37  	flags.BoolVarP(&podInspectCommand.Latest, "latest", "l", false, "Act on the latest pod podman is aware of")
    38  
    39  	markFlagHiddenForRemoteClient("latest", flags)
    40  }
    41  
    42  func podInspectCmd(c *cliconfig.PodInspectValues) error {
    43  	var (
    44  		pod *adapter.Pod
    45  	)
    46  	args := c.InputArgs
    47  
    48  	if len(args) < 1 && !c.Latest {
    49  		return errors.Errorf("you must provide the name or id of a pod")
    50  	}
    51  
    52  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    53  	if err != nil {
    54  		return errors.Wrapf(err, "could not get runtime")
    55  	}
    56  	defer runtime.DeferredShutdown(false)
    57  
    58  	if c.Latest {
    59  		pod, err = runtime.GetLatestPod()
    60  		if err != nil {
    61  			return errors.Wrapf(err, "unable to get latest pod")
    62  		}
    63  	} else {
    64  		pod, err = runtime.LookupPod(args[0])
    65  		if err != nil {
    66  			return err
    67  		}
    68  	}
    69  
    70  	podInspectData, err := pod.Inspect()
    71  	if err != nil {
    72  		return err
    73  	}
    74  	b, err := json.MarshalIndent(&podInspectData, "", "     ")
    75  	if err != nil {
    76  		return err
    77  	}
    78  	fmt.Println(string(b))
    79  	return nil
    80  }