github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/volume_inspect.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/containers/buildah/pkg/formats" 7 "github.com/containers/libpod/cmd/podman/cliconfig" 8 "github.com/containers/libpod/pkg/adapter" 9 "github.com/pkg/errors" 10 "github.com/spf13/cobra" 11 ) 12 13 var ( 14 volumeInspectCommand cliconfig.VolumeInspectValues 15 volumeInspectDescription = `Display detailed information on one or more volumes. 16 17 Use a Go template to change the format from JSON.` 18 _volumeInspectCommand = &cobra.Command{ 19 Use: "inspect [flags] VOLUME [VOLUME...]", 20 Short: "Display detailed information on one or more volumes", 21 Long: volumeInspectDescription, 22 RunE: func(cmd *cobra.Command, args []string) error { 23 volumeInspectCommand.InputArgs = args 24 volumeInspectCommand.GlobalFlags = MainGlobalOpts 25 volumeInspectCommand.Remote = remoteclient 26 return volumeInspectCmd(&volumeInspectCommand) 27 }, 28 Example: `podman volume inspect myvol 29 podman volume inspect --all 30 podman volume inspect --format "{{.Driver}} {{.Scope}}" myvol`, 31 } 32 ) 33 34 func init() { 35 volumeInspectCommand.Command = _volumeInspectCommand 36 volumeInspectCommand.SetHelpTemplate(HelpTemplate()) 37 volumeInspectCommand.SetUsageTemplate(UsageTemplate()) 38 flags := volumeInspectCommand.Flags() 39 flags.BoolVarP(&volumeInspectCommand.All, "all", "a", false, "Inspect all volumes") 40 flags.StringVarP(&volumeInspectCommand.Format, "format", "f", "json", "Format volume output using Go template") 41 42 } 43 44 func volumeInspectCmd(c *cliconfig.VolumeInspectValues) error { 45 if (c.All && len(c.InputArgs) > 0) || (!c.All && len(c.InputArgs) < 1) { 46 return errors.New("provide one or more volume names or use --all") 47 } 48 49 runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand) 50 if err != nil { 51 return errors.Wrapf(err, "error creating libpod runtime") 52 } 53 defer runtime.DeferredShutdown(false) 54 55 vols, err := runtime.InspectVolumes(getContext(), c) 56 if err != nil { 57 return err 58 } 59 60 switch c.Format { 61 case "", formats.JSONString: 62 // Normal format - JSON string 63 jsonOut, err := json.MarshalIndent(vols, "", " ") 64 if err != nil { 65 return errors.Wrapf(err, "error marshalling inspect JSON") 66 } 67 fmt.Println(string(jsonOut)) 68 default: 69 // It's a Go template. 70 interfaces := make([]interface{}, len(vols)) 71 for i, vol := range vols { 72 interfaces[i] = vol 73 } 74 out := formats.StdoutTemplateArray{Output: interfaces, Template: c.Format} 75 return out.Out() 76 } 77 78 return nil 79 }