github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/api/client/volume/list.go (about) 1 package volume 2 3 import ( 4 "sort" 5 6 "golang.org/x/net/context" 7 8 "github.com/docker/docker/api/client" 9 "github.com/docker/docker/api/client/formatter" 10 "github.com/docker/docker/cli" 11 "github.com/docker/engine-api/types" 12 "github.com/docker/engine-api/types/filters" 13 "github.com/spf13/cobra" 14 ) 15 16 type byVolumeName []*types.Volume 17 18 func (r byVolumeName) Len() int { return len(r) } 19 func (r byVolumeName) Swap(i, j int) { r[i], r[j] = r[j], r[i] } 20 func (r byVolumeName) Less(i, j int) bool { 21 return r[i].Name < r[j].Name 22 } 23 24 type listOptions struct { 25 quiet bool 26 format string 27 filter []string 28 } 29 30 func newListCommand(dockerCli *client.DockerCli) *cobra.Command { 31 var opts listOptions 32 33 cmd := &cobra.Command{ 34 Use: "ls [OPTIONS]", 35 Aliases: []string{"list"}, 36 Short: "List volumes", 37 Long: listDescription, 38 Args: cli.NoArgs, 39 RunE: func(cmd *cobra.Command, args []string) error { 40 return runList(dockerCli, opts) 41 }, 42 } 43 44 flags := cmd.Flags() 45 flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display volume names") 46 flags.StringVar(&opts.format, "format", "", "Pretty-print networks using a Go template") 47 flags.StringSliceVarP(&opts.filter, "filter", "f", []string{}, "Provide filter values (i.e. 'dangling=true')") 48 49 return cmd 50 } 51 52 func runList(dockerCli *client.DockerCli, opts listOptions) error { 53 client := dockerCli.Client() 54 55 volFilterArgs := filters.NewArgs() 56 for _, f := range opts.filter { 57 var err error 58 volFilterArgs, err = filters.ParseFlag(f, volFilterArgs) 59 if err != nil { 60 return err 61 } 62 } 63 64 volumes, err := client.VolumeList(context.Background(), volFilterArgs) 65 if err != nil { 66 return err 67 } 68 69 f := opts.format 70 if len(f) == 0 { 71 if len(dockerCli.ConfigFile().VolumesFormat) > 0 && !opts.quiet { 72 f = dockerCli.ConfigFile().VolumesFormat 73 } else { 74 f = "table" 75 } 76 } 77 78 sort.Sort(byVolumeName(volumes.Volumes)) 79 80 volumeCtx := formatter.VolumeContext{ 81 Context: formatter.Context{ 82 Output: dockerCli.Out(), 83 Format: f, 84 Quiet: opts.quiet, 85 }, 86 Volumes: volumes.Volumes, 87 } 88 89 volumeCtx.Write() 90 91 return nil 92 } 93 94 var listDescription = ` 95 96 Lists all the volumes Docker knows about. You can filter using the **-f** or 97 **--filter** flag. The filtering format is a **key=value** pair. To specify 98 more than one filter, pass multiple flags (for example, 99 **--filter "foo=bar" --filter "bif=baz"**) 100 101 There is a single supported filter **dangling=value** which takes a boolean of 102 **true** or **false**. 103 104 `