volume.go 5.5 KB

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