github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/volume.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"text/tabwriter"
     6  
     7  	Cli "github.com/docker/docker/cli"
     8  	"github.com/docker/docker/opts"
     9  	flag "github.com/docker/docker/pkg/mflag"
    10  	"github.com/docker/engine-api/types"
    11  	"github.com/docker/engine-api/types/filters"
    12  )
    13  
    14  // CmdVolume is the parent subcommand for all volume commands
    15  //
    16  // Usage: docker volume <COMMAND> <OPTS>
    17  func (cli *DockerCli) CmdVolume(args ...string) error {
    18  	description := Cli.DockerCommands["volume"].Description + "\n\nCommands:\n"
    19  	commands := [][]string{
    20  		{"create", "Create a volume"},
    21  		{"inspect", "Return low-level information on a volume"},
    22  		{"ls", "List volumes"},
    23  		{"rm", "Remove a volume"},
    24  	}
    25  
    26  	for _, cmd := range commands {
    27  		description += fmt.Sprintf("  %-25.25s%s\n", cmd[0], cmd[1])
    28  	}
    29  
    30  	description += "\nRun 'docker volume COMMAND --help' for more information on a command"
    31  	cmd := Cli.Subcmd("volume", []string{"[COMMAND]"}, description, false)
    32  
    33  	cmd.Require(flag.Exact, 0)
    34  	err := cmd.ParseFlags(args, true)
    35  	cmd.Usage()
    36  	return err
    37  }
    38  
    39  // CmdVolumeLs outputs a list of Docker volumes.
    40  //
    41  // Usage: docker volume ls [OPTIONS]
    42  func (cli *DockerCli) CmdVolumeLs(args ...string) error {
    43  	cmd := Cli.Subcmd("volume ls", nil, "List volumes", true)
    44  
    45  	quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only display volume names")
    46  	flFilter := opts.NewListOpts(nil)
    47  	cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values (i.e. 'dangling=true')")
    48  
    49  	cmd.Require(flag.Exact, 0)
    50  	cmd.ParseFlags(args, true)
    51  
    52  	volFilterArgs := filters.NewArgs()
    53  	for _, f := range flFilter.GetAll() {
    54  		var err error
    55  		volFilterArgs, err = filters.ParseFlag(f, volFilterArgs)
    56  		if err != nil {
    57  			return err
    58  		}
    59  	}
    60  
    61  	volumes, err := cli.client.VolumeList(volFilterArgs)
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
    67  	if !*quiet {
    68  		for _, warn := range volumes.Warnings {
    69  			fmt.Fprintln(cli.err, warn)
    70  		}
    71  		fmt.Fprintf(w, "DRIVER \tVOLUME NAME")
    72  		fmt.Fprintf(w, "\n")
    73  	}
    74  
    75  	for _, vol := range volumes.Volumes {
    76  		if *quiet {
    77  			fmt.Fprintln(w, vol.Name)
    78  			continue
    79  		}
    80  		fmt.Fprintf(w, "%s\t%s\n", vol.Driver, vol.Name)
    81  	}
    82  	w.Flush()
    83  	return nil
    84  }
    85  
    86  // CmdVolumeInspect displays low-level information on one or more volumes.
    87  //
    88  // Usage: docker volume inspect [OPTIONS] VOLUME [VOLUME...]
    89  func (cli *DockerCli) CmdVolumeInspect(args ...string) error {
    90  	cmd := Cli.Subcmd("volume inspect", []string{"VOLUME [VOLUME...]"}, "Return low-level information on a volume", true)
    91  	tmplStr := cmd.String([]string{"f", "-format"}, "", "Format the output using the given go template")
    92  
    93  	cmd.Require(flag.Min, 1)
    94  	cmd.ParseFlags(args, true)
    95  
    96  	if err := cmd.Parse(args); err != nil {
    97  		return nil
    98  	}
    99  
   100  	inspectSearcher := func(name string) (interface{}, []byte, error) {
   101  		i, err := cli.client.VolumeInspect(name)
   102  		return i, nil, err
   103  	}
   104  
   105  	return cli.inspectElements(*tmplStr, cmd.Args(), inspectSearcher)
   106  }
   107  
   108  // CmdVolumeCreate creates a new volume.
   109  //
   110  // Usage: docker volume create [OPTIONS]
   111  func (cli *DockerCli) CmdVolumeCreate(args ...string) error {
   112  	cmd := Cli.Subcmd("volume create", nil, "Create a volume", true)
   113  	flDriver := cmd.String([]string{"d", "-driver"}, "local", "Specify volume driver name")
   114  	flName := cmd.String([]string{"-name"}, "", "Specify volume name")
   115  
   116  	flDriverOpts := opts.NewMapOpts(nil, nil)
   117  	cmd.Var(flDriverOpts, []string{"o", "-opt"}, "Set driver specific options")
   118  
   119  	cmd.Require(flag.Exact, 0)
   120  	cmd.ParseFlags(args, true)
   121  
   122  	volReq := types.VolumeCreateRequest{
   123  		Driver:     *flDriver,
   124  		DriverOpts: flDriverOpts.GetAll(),
   125  		Name:       *flName,
   126  	}
   127  
   128  	vol, err := cli.client.VolumeCreate(volReq)
   129  	if err != nil {
   130  		return err
   131  	}
   132  
   133  	fmt.Fprintf(cli.out, "%s\n", vol.Name)
   134  	return nil
   135  }
   136  
   137  // CmdVolumeRm removes one or more volumes.
   138  //
   139  // Usage: docker volume rm VOLUME [VOLUME...]
   140  func (cli *DockerCli) CmdVolumeRm(args ...string) error {
   141  	cmd := Cli.Subcmd("volume rm", []string{"VOLUME [VOLUME...]"}, "Remove a volume", true)
   142  	cmd.Require(flag.Min, 1)
   143  	cmd.ParseFlags(args, true)
   144  
   145  	var status = 0
   146  
   147  	for _, name := range cmd.Args() {
   148  		if err := cli.client.VolumeRemove(name); err != nil {
   149  			fmt.Fprintf(cli.err, "%s\n", err)
   150  			status = 1
   151  			continue
   152  		}
   153  		fmt.Fprintf(cli.out, "%s\n", name)
   154  	}
   155  
   156  	if status != 0 {
   157  		return Cli.StatusError{StatusCode: status}
   158  	}
   159  	return nil
   160  }