github.com/portworx/docker@v1.12.1/api/client/volume/list.go (about)

     1  package volume
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"text/tabwriter"
     7  
     8  	"golang.org/x/net/context"
     9  
    10  	"github.com/docker/docker/api/client"
    11  	"github.com/docker/docker/cli"
    12  	"github.com/docker/engine-api/types"
    13  	"github.com/docker/engine-api/types/filters"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  type byVolumeName []*types.Volume
    18  
    19  func (r byVolumeName) Len() int      { return len(r) }
    20  func (r byVolumeName) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
    21  func (r byVolumeName) Less(i, j int) bool {
    22  	return r[i].Name < r[j].Name
    23  }
    24  
    25  type listOptions struct {
    26  	quiet  bool
    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.StringSliceVarP(&opts.filter, "filter", "f", []string{}, "Provide filter values (i.e. 'dangling=true')")
    47  
    48  	return cmd
    49  }
    50  
    51  func runList(dockerCli *client.DockerCli, opts listOptions) error {
    52  	client := dockerCli.Client()
    53  
    54  	volFilterArgs := filters.NewArgs()
    55  	for _, f := range opts.filter {
    56  		var err error
    57  		volFilterArgs, err = filters.ParseFlag(f, volFilterArgs)
    58  		if err != nil {
    59  			return err
    60  		}
    61  	}
    62  
    63  	volumes, err := client.VolumeList(context.Background(), volFilterArgs)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	w := tabwriter.NewWriter(dockerCli.Out(), 20, 1, 3, ' ', 0)
    69  	if !opts.quiet {
    70  		for _, warn := range volumes.Warnings {
    71  			fmt.Fprintln(dockerCli.Err(), warn)
    72  		}
    73  		fmt.Fprintf(w, "DRIVER \tVOLUME NAME")
    74  		fmt.Fprintf(w, "\n")
    75  	}
    76  
    77  	sort.Sort(byVolumeName(volumes.Volumes))
    78  	for _, vol := range volumes.Volumes {
    79  		if opts.quiet {
    80  			fmt.Fprintln(w, vol.Name)
    81  			continue
    82  		}
    83  		fmt.Fprintf(w, "%s\t%s\n", vol.Driver, vol.Name)
    84  	}
    85  	w.Flush()
    86  	return nil
    87  }
    88  
    89  var listDescription = `
    90  
    91  Lists all the volumes Docker knows about. You can filter using the **-f** or
    92  **--filter** flag. The filtering format is a **key=value** pair. To specify
    93  more than one filter,  pass multiple flags (for example,
    94  **--filter "foo=bar" --filter "bif=baz"**)
    95  
    96  There is a single supported filter **dangling=value** which takes a boolean of
    97  **true** or **false**.
    98  
    99  `