github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/volume_list.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/containerd/nerdctl/pkg/inspecttypes/native" 23 24 "github.com/spf13/cobra" 25 ) 26 27 func newVolumeLsCommand() *cobra.Command { 28 volumeLsCommand := &cobra.Command{ 29 Use: "ls", 30 Aliases: []string{"list"}, 31 Short: "List volumes", 32 RunE: volumeLsAction, 33 SilenceUsage: true, 34 SilenceErrors: true, 35 } 36 37 volumeLsCommand.Flags().BoolP("quiet", "q", false, "Only display volume names") 38 // Alias "-f" is reserved for "--filter" 39 volumeLsCommand.Flags().String("format", "", "Format the output using the given go template") 40 volumeLsCommand.Flags().BoolP("size", "s", false, "Display the disk usage of volumes. Can be slow with volumes having loads of directories.") 41 volumeLsCommand.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 42 return []string{"json", "table", "wide"}, cobra.ShellCompDirectiveNoFileComp 43 }) 44 volumeLsCommand.Flags().StringSliceP("filter", "f", []string{}, "Filter matches volumes based on given conditions") 45 return volumeLsCommand 46 } 47 48 func processVolumeLsOptions(cmd *cobra.Command) (types.VolumeListOptions, error) { 49 globalOptions, err := processRootCmdFlags(cmd) 50 if err != nil { 51 return types.VolumeListOptions{}, err 52 } 53 quiet, err := cmd.Flags().GetBool("quiet") 54 if err != nil { 55 return types.VolumeListOptions{}, err 56 } 57 format, err := cmd.Flags().GetString("format") 58 if err != nil { 59 return types.VolumeListOptions{}, err 60 } 61 size, err := cmd.Flags().GetBool("size") 62 if err != nil { 63 return types.VolumeListOptions{}, err 64 } 65 filters, err := cmd.Flags().GetStringSlice("filter") 66 if err != nil { 67 return types.VolumeListOptions{}, err 68 } 69 return types.VolumeListOptions{ 70 GOptions: globalOptions, 71 Quiet: quiet, 72 Format: format, 73 Size: size, 74 Filters: filters, 75 Stdout: cmd.OutOrStdout(), 76 }, nil 77 } 78 79 func volumeLsAction(cmd *cobra.Command, args []string) error { 80 options, err := processVolumeLsOptions(cmd) 81 if err != nil { 82 return err 83 } 84 return volume.List(options) 85 } 86 87 func getVolumes(cmd *cobra.Command, globalOptions types.GlobalCommandOptions) (map[string]native.Volume, error) { 88 volumeSize, err := cmd.Flags().GetBool("size") 89 if err != nil { 90 // The `nerdctl volume rm` does not have the flag `size`, so set it to false as the default value. 91 volumeSize = false 92 } 93 return volume.Volumes(globalOptions.Namespace, globalOptions.DataRoot, globalOptions.Address, volumeSize, nil) 94 }