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

     1  package volumes
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"html/template"
     7  	"os"
     8  
     9  	"github.com/containers/buildah/pkg/formats"
    10  	"github.com/containers/libpod/cmd/podmanV2/registry"
    11  	"github.com/containers/libpod/pkg/domain/entities"
    12  	"github.com/pkg/errors"
    13  	"github.com/spf13/cobra"
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  var (
    18  	volumeInspectDescription = `Display detailed information on one or more volumes.
    19  
    20    Use a Go template to change the format from JSON.`
    21  	inspectCommand = &cobra.Command{
    22  		Use:   "inspect [flags] VOLUME [VOLUME...]",
    23  		Short: "Display detailed information on one or more volumes",
    24  		Long:  volumeInspectDescription,
    25  		RunE:  inspect,
    26  		Example: `podman volume inspect myvol
    27    podman volume inspect --all
    28    podman volume inspect --format "{{.Driver}} {{.Scope}}" myvol`,
    29  	}
    30  )
    31  
    32  var (
    33  	inspectOpts   = entities.VolumeInspectOptions{}
    34  	inspectFormat string
    35  )
    36  
    37  func init() {
    38  	registry.Commands = append(registry.Commands, registry.CliCommand{
    39  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    40  		Command: inspectCommand,
    41  		Parent:  volumeCmd,
    42  	})
    43  	flags := inspectCommand.Flags()
    44  	flags.BoolVarP(&inspectOpts.All, "all", "a", false, "Inspect all volumes")
    45  	flags.StringVarP(&inspectFormat, "format", "f", "json", "Format volume output using Go template")
    46  }
    47  
    48  func inspect(cmd *cobra.Command, args []string) error {
    49  	if (inspectOpts.All && len(args) > 0) || (!inspectOpts.All && len(args) < 1) {
    50  		return errors.New("provide one or more volume names or use --all")
    51  	}
    52  	responses, err := registry.ContainerEngine().VolumeInspect(context.Background(), args, inspectOpts)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	switch inspectFormat {
    57  	case "", formats.JSONString:
    58  		jsonOut, err := json.MarshalIndent(responses, "", "     ")
    59  		if err != nil {
    60  			return errors.Wrapf(err, "error marshalling inspect JSON")
    61  		}
    62  		fmt.Println(string(jsonOut))
    63  	default:
    64  		tmpl, err := template.New("volumeInspect").Parse(inspectFormat)
    65  		if err != nil {
    66  			return err
    67  		}
    68  		if err := tmpl.Execute(os.Stdout, responses); err != nil {
    69  			return err
    70  		}
    71  	}
    72  	return nil
    73  
    74  }