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