github.com/kunnos/engine@v1.13.1/cli/command/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/types"
     9  	"github.com/docker/docker/cli"
    10  	"github.com/docker/docker/cli/command"
    11  	"github.com/docker/docker/cli/command/formatter"
    12  	"github.com/docker/docker/opts"
    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 opts.FilterOpt
    28  }
    29  
    30  func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
    31  	opts := listOptions{filter: opts.NewFilterOpt()}
    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 volumes using a Go template")
    47  	flags.VarP(&opts.filter, "filter", "f", "Provide filter values (e.g. 'dangling=true')")
    48  
    49  	return cmd
    50  }
    51  
    52  func runList(dockerCli *command.DockerCli, opts listOptions) error {
    53  	client := dockerCli.Client()
    54  	volumes, err := client.VolumeList(context.Background(), opts.filter.Value())
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	format := opts.format
    60  	if len(format) == 0 {
    61  		if len(dockerCli.ConfigFile().VolumesFormat) > 0 && !opts.quiet {
    62  			format = dockerCli.ConfigFile().VolumesFormat
    63  		} else {
    64  			format = formatter.TableFormatKey
    65  		}
    66  	}
    67  
    68  	sort.Sort(byVolumeName(volumes.Volumes))
    69  
    70  	volumeCtx := formatter.Context{
    71  		Output: dockerCli.Out(),
    72  		Format: formatter.NewVolumeFormat(format, opts.quiet),
    73  	}
    74  	return formatter.VolumeWrite(volumeCtx, volumes.Volumes)
    75  }
    76  
    77  var listDescription = `
    78  
    79  Lists all the volumes Docker manages. You can filter using the **-f** or
    80  **--filter** flag. The filtering format is a **key=value** pair. To specify
    81  more than one filter,  pass multiple flags (for example,
    82  **--filter "foo=bar" --filter "bif=baz"**)
    83  
    84  The currently supported filters are:
    85  
    86  * **dangling** (boolean - **true** or **false**, **1** or **0**)
    87  * **driver** (a volume driver's name)
    88  * **label** (**label=<key>** or **label=<key>=<value>**)
    89  * **name** (a volume's name)
    90  
    91  `