github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/pods/inspect.go (about)

     1  package pods
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/containers/libpod/cmd/podmanV2/registry"
     8  	"github.com/containers/libpod/pkg/domain/entities"
     9  	jsoniter "github.com/json-iterator/go"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var (
    15  	inspectOptions = entities.PodInspectOptions{}
    16  )
    17  
    18  var (
    19  	inspectDescription = fmt.Sprintf(`Display the configuration for a pod by name or id
    20  
    21  	By default, this will render all results in a JSON array.`)
    22  
    23  	inspectCmd = &cobra.Command{
    24  		Use:     "inspect [flags] POD [POD...]",
    25  		Short:   "Displays a pod configuration",
    26  		Long:    inspectDescription,
    27  		RunE:    inspect,
    28  		Example: `podman pod inspect podID`,
    29  	}
    30  )
    31  
    32  func init() {
    33  	registry.Commands = append(registry.Commands, registry.CliCommand{
    34  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    35  		Command: inspectCmd,
    36  		Parent:  podCmd,
    37  	})
    38  	flags := inspectCmd.Flags()
    39  	flags.BoolVarP(&inspectOptions.Latest, "latest", "l", false, "Act on the latest pod podman is aware of")
    40  	if registry.IsRemote() {
    41  		_ = flags.MarkHidden("latest")
    42  	}
    43  }
    44  
    45  func inspect(cmd *cobra.Command, args []string) error {
    46  
    47  	if len(args) < 1 && !inspectOptions.Latest {
    48  		return errors.Errorf("you must provide the name or id of a running pod")
    49  	}
    50  
    51  	if !inspectOptions.Latest {
    52  		inspectOptions.NameOrID = args[0]
    53  	}
    54  	responses, err := registry.ContainerEngine().PodInspect(context.Background(), inspectOptions)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	b, err := jsoniter.MarshalIndent(responses, "", "  ")
    59  	if err != nil {
    60  		return err
    61  	}
    62  	fmt.Println(string(b))
    63  	return nil
    64  }