2015-06-12 09:25:32 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-02-17 03:59:53 -05:00
|
|
|
"sort"
|
2015-06-12 09:25:32 -04:00
|
|
|
"text/tabwriter"
|
|
|
|
|
2016-03-16 15:19:22 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-06-12 09:25:32 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/engine-api/types/filters"
|
2015-06-12 09:25:32 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdVolume is the parent subcommand for all volume commands
|
|
|
|
//
|
|
|
|
// Usage: docker volume <COMMAND> <OPTS>
|
|
|
|
func (cli *DockerCli) CmdVolume(args ...string) error {
|
2015-10-08 08:46:21 -04:00
|
|
|
description := Cli.DockerCommands["volume"].Description + "\n\nCommands:\n"
|
2015-06-12 09:25:32 -04:00
|
|
|
commands := [][]string{
|
|
|
|
{"create", "Create a volume"},
|
|
|
|
{"inspect", "Return low-level information on a volume"},
|
|
|
|
{"ls", "List volumes"},
|
|
|
|
{"rm", "Remove a volume"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cmd := range commands {
|
|
|
|
description += fmt.Sprintf(" %-25.25s%s\n", cmd[0], cmd[1])
|
|
|
|
}
|
|
|
|
|
2015-09-08 17:19:27 -04:00
|
|
|
description += "\nRun 'docker volume COMMAND --help' for more information on a command"
|
2015-10-07 12:22:08 -04:00
|
|
|
cmd := Cli.Subcmd("volume", []string{"[COMMAND]"}, description, false)
|
2015-06-12 09:25:32 -04:00
|
|
|
|
2015-10-07 12:22:08 -04:00
|
|
|
cmd.Require(flag.Exact, 0)
|
|
|
|
err := cmd.ParseFlags(args, true)
|
|
|
|
cmd.Usage()
|
|
|
|
return err
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CmdVolumeLs outputs a list of Docker volumes.
|
|
|
|
//
|
|
|
|
// Usage: docker volume ls [OPTIONS]
|
|
|
|
func (cli *DockerCli) CmdVolumeLs(args ...string) error {
|
|
|
|
cmd := Cli.Subcmd("volume ls", nil, "List volumes", true)
|
|
|
|
|
|
|
|
quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only display volume names")
|
|
|
|
flFilter := opts.NewListOpts(nil)
|
|
|
|
cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values (i.e. 'dangling=true')")
|
|
|
|
|
|
|
|
cmd.Require(flag.Exact, 0)
|
|
|
|
cmd.ParseFlags(args, true)
|
|
|
|
|
2015-11-25 20:27:11 -05:00
|
|
|
volFilterArgs := filters.NewArgs()
|
2015-06-12 09:25:32 -04:00
|
|
|
for _, f := range flFilter.GetAll() {
|
|
|
|
var err error
|
|
|
|
volFilterArgs, err = filters.ParseFlag(f, volFilterArgs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 15:19:22 -04:00
|
|
|
volumes, err := cli.client.VolumeList(context.Background(), volFilterArgs)
|
2015-06-12 09:25:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
|
|
|
if !*quiet {
|
2015-09-23 16:29:14 -04:00
|
|
|
for _, warn := range volumes.Warnings {
|
|
|
|
fmt.Fprintln(cli.err, warn)
|
|
|
|
}
|
2015-06-12 09:25:32 -04:00
|
|
|
fmt.Fprintf(w, "DRIVER \tVOLUME NAME")
|
|
|
|
fmt.Fprintf(w, "\n")
|
|
|
|
}
|
|
|
|
|
2016-02-17 03:59:53 -05:00
|
|
|
sort.Sort(byVolumeName(volumes.Volumes))
|
2015-06-12 09:25:32 -04:00
|
|
|
for _, vol := range volumes.Volumes {
|
|
|
|
if *quiet {
|
|
|
|
fmt.Fprintln(w, vol.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "%s\t%s\n", vol.Driver, vol.Name)
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-17 03:59:53 -05: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
|
|
|
|
}
|
|
|
|
|
2015-06-12 09:25:32 -04:00
|
|
|
// CmdVolumeInspect displays low-level information on one or more volumes.
|
|
|
|
//
|
|
|
|
// Usage: docker volume inspect [OPTIONS] VOLUME [VOLUME...]
|
|
|
|
func (cli *DockerCli) CmdVolumeInspect(args ...string) error {
|
2015-09-08 12:02:16 -04:00
|
|
|
cmd := Cli.Subcmd("volume inspect", []string{"VOLUME [VOLUME...]"}, "Return low-level information on a volume", true)
|
2015-09-08 17:19:27 -04:00
|
|
|
tmplStr := cmd.String([]string{"f", "-format"}, "", "Format the output using the given go template")
|
2015-06-12 09:25:32 -04:00
|
|
|
|
|
|
|
cmd.Require(flag.Min, 1)
|
|
|
|
cmd.ParseFlags(args, true)
|
|
|
|
|
2015-09-08 17:19:27 -04:00
|
|
|
if err := cmd.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-07 22:04:38 -05:00
|
|
|
inspectSearcher := func(name string) (interface{}, []byte, error) {
|
2016-03-16 15:19:22 -04:00
|
|
|
i, err := cli.client.VolumeInspect(context.Background(), name)
|
2015-12-07 22:04:38 -05:00
|
|
|
return i, nil, err
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
2015-12-07 22:04:38 -05:00
|
|
|
return cli.inspectElements(*tmplStr, cmd.Args(), inspectSearcher)
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
// CmdVolumeCreate creates a new volume.
|
2015-06-12 09:25:32 -04:00
|
|
|
//
|
|
|
|
// Usage: docker volume create [OPTIONS]
|
|
|
|
func (cli *DockerCli) CmdVolumeCreate(args ...string) error {
|
|
|
|
cmd := Cli.Subcmd("volume create", nil, "Create a volume", true)
|
|
|
|
flDriver := cmd.String([]string{"d", "-driver"}, "local", "Specify volume driver name")
|
|
|
|
flName := cmd.String([]string{"-name"}, "", "Specify volume name")
|
|
|
|
|
|
|
|
flDriverOpts := opts.NewMapOpts(nil, nil)
|
|
|
|
cmd.Var(flDriverOpts, []string{"o", "-opt"}, "Set driver specific options")
|
|
|
|
|
|
|
|
cmd.Require(flag.Exact, 0)
|
|
|
|
cmd.ParseFlags(args, true)
|
|
|
|
|
2015-12-04 15:17:23 -05:00
|
|
|
volReq := types.VolumeCreateRequest{
|
2015-06-12 09:25:32 -04:00
|
|
|
Driver: *flDriver,
|
|
|
|
DriverOpts: flDriverOpts.GetAll(),
|
2015-12-04 15:17:23 -05:00
|
|
|
Name: *flName,
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
2016-03-16 15:19:22 -04:00
|
|
|
vol, err := cli.client.VolumeCreate(context.Background(), volReq)
|
2015-06-12 09:25:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(cli.out, "%s\n", vol.Name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
// CmdVolumeRm removes one or more volumes.
|
2015-06-12 09:25:32 -04:00
|
|
|
//
|
|
|
|
// Usage: docker volume rm VOLUME [VOLUME...]
|
|
|
|
func (cli *DockerCli) CmdVolumeRm(args ...string) error {
|
2015-09-08 12:02:16 -04:00
|
|
|
cmd := Cli.Subcmd("volume rm", []string{"VOLUME [VOLUME...]"}, "Remove a volume", true)
|
2015-06-12 09:25:32 -04:00
|
|
|
cmd.Require(flag.Min, 1)
|
|
|
|
cmd.ParseFlags(args, true)
|
|
|
|
|
|
|
|
var status = 0
|
2015-09-23 16:29:14 -04:00
|
|
|
|
2015-06-12 09:25:32 -04:00
|
|
|
for _, name := range cmd.Args() {
|
2016-03-16 15:19:22 -04:00
|
|
|
if err := cli.client.VolumeRemove(context.Background(), name); err != nil {
|
2015-06-12 09:25:32 -04:00
|
|
|
fmt.Fprintf(cli.err, "%s\n", err)
|
|
|
|
status = 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Fprintf(cli.out, "%s\n", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != 0 {
|
|
|
|
return Cli.StatusError{StatusCode: status}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|