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"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2021-03-09 18:45:56 -05:00
|
|
|
flagsHelper "github.com/docker/cli/cli/flags"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2023-02-07 10:52:18 -05:00
|
|
|
"github.com/docker/docker/api/types/volume"
|
2020-08-28 08:35:09 -04:00
|
|
|
"github.com/fvbommel/sortorder"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2021-02-23 09:23:53 -05:00
|
|
|
const (
|
|
|
|
clusterTableFormat = "table {{.Name}}\t{{.Group}}\t{{.Driver}}\t{{.Availability}}\t{{.Status}}"
|
|
|
|
)
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
type listOptions struct {
|
2021-02-23 09:23:53 -05:00
|
|
|
quiet bool
|
|
|
|
format string
|
|
|
|
cluster bool
|
|
|
|
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
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
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")
|
2021-03-09 18:45:56 -05:00
|
|
|
flags.StringVar(&options.format, "format", "", flagsHelper.FormatHelp)
|
2023-01-03 05:12:24 -05:00
|
|
|
flags.VarP(&options.filter, "filter", "f", `Provide filter values (e.g. "dangling=true")`)
|
2021-02-23 09:23:53 -05:00
|
|
|
flags.BoolVar(&options.cluster, "cluster", false, "Display only cluster volumes, and use cluster volume list formatting")
|
2022-05-13 13:48:24 -04:00
|
|
|
flags.SetAnnotation("cluster", "version", []string{"1.42"})
|
|
|
|
flags.SetAnnotation("cluster", "swarm", []string{"manager"})
|
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()
|
2023-02-07 10:52:18 -05:00
|
|
|
volumes, err := client.VolumeList(context.Background(), volume.ListOptions{Filters: 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
|
2021-02-23 09:23:53 -05:00
|
|
|
if len(format) == 0 && !options.cluster {
|
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
|
|
|
}
|
2021-02-23 09:23:53 -05:00
|
|
|
} else if options.cluster {
|
|
|
|
// TODO(dperny): write server-side filter for cluster volumes. For this
|
|
|
|
// proof of concept, we'll just filter out non-cluster volumes here
|
|
|
|
|
|
|
|
// trick for filtering in place
|
|
|
|
n := 0
|
2023-02-07 10:52:18 -05:00
|
|
|
for _, vol := range volumes.Volumes {
|
|
|
|
if vol.ClusterVolume != nil {
|
|
|
|
volumes.Volumes[n] = vol
|
2021-02-23 09:23:53 -05:00
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
volumes.Volumes = volumes.Volumes[:n]
|
|
|
|
if !options.quiet {
|
|
|
|
format = clusterTableFormat
|
|
|
|
} else {
|
|
|
|
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 {
|
2018-08-08 11:57:39 -04:00
|
|
|
return sortorder.NaturalLess(volumes.Volumes[i].Name, volumes.Volumes[j].Name)
|
2018-07-06 15:49:10 -04:00
|
|
|
})
|
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
|
|
|
}
|