2016-09-08 13:11:39 -04:00
|
|
|
package volume
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/docker/docker/cli/command/formatter"
|
2016-09-13 14:53:11 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
2017-02-27 12:39:35 -05:00
|
|
|
"golang.org/x/net/context"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type byVolumeName []*types.Volume
|
|
|
|
|
|
|
|
func (r byVolumeName) Len() int { return len(r) }
|
|
|
|
func (r byVolumeName) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
func (r byVolumeName) Less(i, j int) bool {
|
|
|
|
return r[i].Name < r[j].Name
|
|
|
|
}
|
|
|
|
|
|
|
|
type listOptions struct {
|
|
|
|
quiet bool
|
|
|
|
format string
|
2016-09-13 14:53:11 -04:00
|
|
|
filter opts.FilterOpt
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-02-27 12:39:35 -05:00
|
|
|
func newListCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-13 14:53:11 -04:00
|
|
|
opts := listOptions{filter: opts.NewFilterOpt()}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ls [OPTIONS]",
|
|
|
|
Aliases: []string{"list"},
|
|
|
|
Short: "List volumes",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runList(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display volume names")
|
|
|
|
flags.StringVar(&opts.format, "format", "", "Pretty-print volumes using a Go template")
|
2016-09-13 14:53:11 -04:00
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Provide filter values (e.g. 'dangling=true')")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-02-27 12:39:35 -05:00
|
|
|
func runList(dockerCli command.Cli, opts listOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
2016-09-13 14:53:11 -04:00
|
|
|
volumes, err := client.VolumeList(context.Background(), opts.filter.Value())
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
format := opts.format
|
|
|
|
if len(format) == 0 {
|
2016-09-08 13:11:39 -04:00
|
|
|
if len(dockerCli.ConfigFile().VolumesFormat) > 0 && !opts.quiet {
|
2016-09-12 16:59:18 -04:00
|
|
|
format = dockerCli.ConfigFile().VolumesFormat
|
2016-09-08 13:11:39 -04:00
|
|
|
} else {
|
2016-09-13 14:21:07 -04:00
|
|
|
format = formatter.TableFormatKey
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(byVolumeName(volumes.Volumes))
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
volumeCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.NewVolumeFormat(format, opts.quiet),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return formatter.VolumeWrite(volumeCtx, volumes.Volumes)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|