github.com/a4a881d4/docker@v1.9.0-rc2/api/client/volume.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"net/url"
     9  	"text/tabwriter"
    10  	"text/template"
    11  
    12  	"github.com/docker/docker/api/types"
    13  	Cli "github.com/docker/docker/cli"
    14  	"github.com/docker/docker/opts"
    15  	flag "github.com/docker/docker/pkg/mflag"
    16  	"github.com/docker/docker/pkg/parsers/filters"
    17  )
    18  
    19  // CmdVolume is the parent subcommand for all volume commands
    20  //
    21  // Usage: docker volume <COMMAND> <OPTS>
    22  func (cli *DockerCli) CmdVolume(args ...string) error {
    23  	description := Cli.DockerCommands["volume"].Description + "\n\nCommands:\n"
    24  	commands := [][]string{
    25  		{"create", "Create a volume"},
    26  		{"inspect", "Return low-level information on a volume"},
    27  		{"ls", "List volumes"},
    28  		{"rm", "Remove a volume"},
    29  	}
    30  
    31  	for _, cmd := range commands {
    32  		description += fmt.Sprintf("  %-25.25s%s\n", cmd[0], cmd[1])
    33  	}
    34  
    35  	description += "\nRun 'docker volume COMMAND --help' for more information on a command"
    36  	cmd := Cli.Subcmd("volume", []string{"[COMMAND]"}, description, false)
    37  
    38  	cmd.Require(flag.Exact, 0)
    39  	err := cmd.ParseFlags(args, true)
    40  	cmd.Usage()
    41  	return err
    42  }
    43  
    44  // CmdVolumeLs outputs a list of Docker volumes.
    45  //
    46  // Usage: docker volume ls [OPTIONS]
    47  func (cli *DockerCli) CmdVolumeLs(args ...string) error {
    48  	cmd := Cli.Subcmd("volume ls", nil, "List volumes", true)
    49  
    50  	quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only display volume names")
    51  	flFilter := opts.NewListOpts(nil)
    52  	cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values (i.e. 'dangling=true')")
    53  
    54  	cmd.Require(flag.Exact, 0)
    55  	cmd.ParseFlags(args, true)
    56  
    57  	volFilterArgs := filters.Args{}
    58  	for _, f := range flFilter.GetAll() {
    59  		var err error
    60  		volFilterArgs, err = filters.ParseFlag(f, volFilterArgs)
    61  		if err != nil {
    62  			return err
    63  		}
    64  	}
    65  
    66  	v := url.Values{}
    67  	if len(volFilterArgs) > 0 {
    68  		filterJSON, err := filters.ToParam(volFilterArgs)
    69  		if err != nil {
    70  			return err
    71  		}
    72  		v.Set("filters", filterJSON)
    73  	}
    74  
    75  	resp, err := cli.call("GET", "/volumes?"+v.Encode(), nil, nil)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	var volumes types.VolumesListResponse
    81  	if err := json.NewDecoder(resp.body).Decode(&volumes); err != nil {
    82  		return err
    83  	}
    84  
    85  	w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
    86  	if !*quiet {
    87  		fmt.Fprintf(w, "DRIVER \tVOLUME NAME")
    88  		fmt.Fprintf(w, "\n")
    89  	}
    90  
    91  	for _, vol := range volumes.Volumes {
    92  		if *quiet {
    93  			fmt.Fprintln(w, vol.Name)
    94  			continue
    95  		}
    96  		fmt.Fprintf(w, "%s\t%s\n", vol.Driver, vol.Name)
    97  	}
    98  	w.Flush()
    99  	return nil
   100  }
   101  
   102  // CmdVolumeInspect displays low-level information on one or more volumes.
   103  //
   104  // Usage: docker volume inspect [OPTIONS] VOLUME [VOLUME...]
   105  func (cli *DockerCli) CmdVolumeInspect(args ...string) error {
   106  	cmd := Cli.Subcmd("volume inspect", []string{"VOLUME [VOLUME...]"}, "Return low-level information on a volume", true)
   107  	tmplStr := cmd.String([]string{"f", "-format"}, "", "Format the output using the given go template")
   108  
   109  	cmd.Require(flag.Min, 1)
   110  	cmd.ParseFlags(args, true)
   111  
   112  	if err := cmd.Parse(args); err != nil {
   113  		return nil
   114  	}
   115  
   116  	var tmpl *template.Template
   117  	if *tmplStr != "" {
   118  		var err error
   119  		tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr)
   120  		if err != nil {
   121  			return err
   122  		}
   123  	}
   124  
   125  	var status = 0
   126  	var volumes []*types.Volume
   127  	for _, name := range cmd.Args() {
   128  		resp, err := cli.call("GET", "/volumes/"+name, nil, nil)
   129  		if err != nil {
   130  			return err
   131  		}
   132  
   133  		var volume types.Volume
   134  		if err := json.NewDecoder(resp.body).Decode(&volume); err != nil {
   135  			fmt.Fprintf(cli.err, "%s\n", err)
   136  			status = 1
   137  			continue
   138  		}
   139  
   140  		if tmpl == nil {
   141  			volumes = append(volumes, &volume)
   142  			continue
   143  		}
   144  
   145  		if err := tmpl.Execute(cli.out, &volume); err != nil {
   146  			if err := tmpl.Execute(cli.out, &volume); err != nil {
   147  				fmt.Fprintf(cli.err, "%s\n", err)
   148  				status = 1
   149  				continue
   150  			}
   151  		}
   152  		io.WriteString(cli.out, "\n")
   153  	}
   154  
   155  	if tmpl != nil {
   156  		return nil
   157  	}
   158  
   159  	b, err := json.MarshalIndent(volumes, "", "    ")
   160  	if err != nil {
   161  		return err
   162  	}
   163  	_, err = io.Copy(cli.out, bytes.NewReader(b))
   164  	if err != nil {
   165  		return err
   166  	}
   167  	io.WriteString(cli.out, "\n")
   168  
   169  	if status != 0 {
   170  		return Cli.StatusError{StatusCode: status}
   171  	}
   172  	return nil
   173  }
   174  
   175  // CmdVolumeCreate creates a new container from a given image.
   176  //
   177  // Usage: docker volume create [OPTIONS]
   178  func (cli *DockerCli) CmdVolumeCreate(args ...string) error {
   179  	cmd := Cli.Subcmd("volume create", nil, "Create a volume", true)
   180  	flDriver := cmd.String([]string{"d", "-driver"}, "local", "Specify volume driver name")
   181  	flName := cmd.String([]string{"-name"}, "", "Specify volume name")
   182  
   183  	flDriverOpts := opts.NewMapOpts(nil, nil)
   184  	cmd.Var(flDriverOpts, []string{"o", "-opt"}, "Set driver specific options")
   185  
   186  	cmd.Require(flag.Exact, 0)
   187  	cmd.ParseFlags(args, true)
   188  
   189  	volReq := &types.VolumeCreateRequest{
   190  		Driver:     *flDriver,
   191  		DriverOpts: flDriverOpts.GetAll(),
   192  	}
   193  
   194  	if *flName != "" {
   195  		volReq.Name = *flName
   196  	}
   197  
   198  	resp, err := cli.call("POST", "/volumes/create", volReq, nil)
   199  	if err != nil {
   200  		return err
   201  	}
   202  
   203  	var vol types.Volume
   204  	if err := json.NewDecoder(resp.body).Decode(&vol); err != nil {
   205  		return err
   206  	}
   207  	fmt.Fprintf(cli.out, "%s\n", vol.Name)
   208  	return nil
   209  }
   210  
   211  // CmdVolumeRm removes one or more containers.
   212  //
   213  // Usage: docker volume rm VOLUME [VOLUME...]
   214  func (cli *DockerCli) CmdVolumeRm(args ...string) error {
   215  	cmd := Cli.Subcmd("volume rm", []string{"VOLUME [VOLUME...]"}, "Remove a volume", true)
   216  	cmd.Require(flag.Min, 1)
   217  	cmd.ParseFlags(args, true)
   218  
   219  	var status = 0
   220  	for _, name := range cmd.Args() {
   221  		_, err := cli.call("DELETE", "/volumes/"+name, nil, nil)
   222  		if err != nil {
   223  			fmt.Fprintf(cli.err, "%s\n", err)
   224  			status = 1
   225  			continue
   226  		}
   227  		fmt.Fprintf(cli.out, "%s\n", name)
   228  	}
   229  
   230  	if status != 0 {
   231  		return Cli.StatusError{StatusCode: status}
   232  	}
   233  	return nil
   234  }