2016-09-08 13:11:39 -04:00
|
|
|
package volume
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"sort"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := 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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
return runList(dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display volume names")
|
|
|
|
flags.StringVar(&options.format, "format", "", "Pretty-print volumes using a Go template")
|
|
|
|
flags.VarP(&options.filter, "filter", "f", "Provide filter values (e.g. 'dangling=true')")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
func runList(dockerCli command.Cli, options listOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
2017-05-15 08:45:19 -04:00
|
|
|
volumes, err := client.VolumeList(context.Background(), options.filter.Value())
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
format := options.format
|
2016-09-12 16:59:18 -04:00
|
|
|
if len(format) == 0 {
|
2017-05-15 08:45:19 -04:00
|
|
|
if len(dockerCli.ConfigFile().VolumesFormat) > 0 && !options.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 15:49:10 -04:00
|
|
|
sort.Slice(volumes.Volumes, func(i, j int) bool {
|
|
|
|
return volumes.Volumes[i].Name < volumes.Volumes[j].Name
|
|
|
|
})
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
volumeCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2017-05-15 08:45:19 -04:00
|
|
|
Format: formatter.NewVolumeFormat(format, options.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
|
|
|
}
|