create.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package volume
  2. import (
  3. "fmt"
  4. "golang.org/x/net/context"
  5. "github.com/docker/docker/api/client"
  6. "github.com/docker/docker/opts"
  7. runconfigopts "github.com/docker/docker/runconfig/opts"
  8. "github.com/docker/engine-api/types"
  9. "github.com/spf13/cobra"
  10. )
  11. type createOptions struct {
  12. name string
  13. driver string
  14. driverOpts opts.MapOpts
  15. labels []string
  16. }
  17. func newCreateCommand(dockerCli *client.DockerCli) *cobra.Command {
  18. var opts createOptions
  19. cmd := &cobra.Command{
  20. Use: "create",
  21. Short: "Create a volume",
  22. RunE: func(cmd *cobra.Command, args []string) error {
  23. return runCreate(dockerCli, opts)
  24. },
  25. }
  26. flags := cmd.Flags()
  27. flags.StringVarP(&opts.driver, "driver", "d", "local", "Specify volume driver name")
  28. flags.StringVar(&opts.name, "name", "", "Specify volume name")
  29. flags.VarP(&opts.driverOpts, "opt", "o", "Set driver specific options")
  30. flags.StringSliceVar(&opts.labels, "label", []string{}, "Set metadata for a volume")
  31. return cmd
  32. }
  33. func runCreate(dockerCli *client.DockerCli, opts createOptions) error {
  34. client := dockerCli.Client()
  35. volReq := types.VolumeCreateRequest{
  36. Driver: opts.driver,
  37. DriverOpts: opts.driverOpts.GetAll(),
  38. Name: opts.name,
  39. Labels: runconfigopts.ConvertKVStringsToMap(opts.labels),
  40. }
  41. vol, err := client.VolumeCreate(context.Background(), volReq)
  42. if err != nil {
  43. return err
  44. }
  45. fmt.Fprintf(dockerCli.Out(), "%s\n", vol.Name)
  46. return nil
  47. }