github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/volume_inspect.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "github.com/containerd/nerdctl/pkg/api/types" 21 "github.com/containerd/nerdctl/pkg/cmd/volume" 22 "github.com/spf13/cobra" 23 ) 24 25 func newVolumeInspectCommand() *cobra.Command { 26 volumeInspectCommand := &cobra.Command{ 27 Use: "inspect [flags] VOLUME [VOLUME...]", 28 Short: "Display detailed information on one or more volumes", 29 Args: cobra.MinimumNArgs(1), 30 RunE: volumeInspectAction, 31 ValidArgsFunction: volumeInspectShellComplete, 32 SilenceUsage: true, 33 SilenceErrors: true, 34 } 35 volumeInspectCommand.Flags().StringP("format", "f", "", "Format the output using the given Go template, e.g, '{{json .}}'") 36 volumeInspectCommand.Flags().BoolP("size", "s", false, "Display the disk usage of the volume") 37 volumeInspectCommand.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 38 return []string{"json"}, cobra.ShellCompDirectiveNoFileComp 39 }) 40 return volumeInspectCommand 41 } 42 43 func processVolumeInspectOptions(cmd *cobra.Command) (types.VolumeInspectOptions, error) { 44 globalOptions, err := processRootCmdFlags(cmd) 45 if err != nil { 46 return types.VolumeInspectOptions{}, err 47 } 48 volumeSize, err := cmd.Flags().GetBool("size") 49 if err != nil { 50 return types.VolumeInspectOptions{}, err 51 } 52 format, err := cmd.Flags().GetString("format") 53 if err != nil { 54 return types.VolumeInspectOptions{}, err 55 } 56 return types.VolumeInspectOptions{ 57 GOptions: globalOptions, 58 Format: format, 59 Size: volumeSize, 60 Stdout: cmd.OutOrStdout(), 61 }, nil 62 } 63 64 func volumeInspectAction(cmd *cobra.Command, args []string) error { 65 options, err := processVolumeInspectOptions(cmd) 66 if err != nil { 67 return err 68 } 69 return volume.Inspect(args, options) 70 } 71 72 func volumeInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 73 // show volume names 74 return shellCompleteVolumeNames(cmd) 75 }