2016-04-19 12:59:48 -04:00
|
|
|
package volume
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/client"
|
2016-05-16 17:20:29 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-04-19 12:59:48 -04:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type createOptions struct {
|
|
|
|
name string
|
|
|
|
driver string
|
|
|
|
driverOpts opts.MapOpts
|
|
|
|
labels []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCreateCommand(dockerCli *client.DockerCli) *cobra.Command {
|
2016-05-16 17:20:29 -04:00
|
|
|
opts := createOptions{
|
|
|
|
driverOpts: *opts.NewMapOpts(nil, nil),
|
|
|
|
}
|
2016-04-19 12:59:48 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "create",
|
|
|
|
Short: "Create a volume",
|
2016-05-26 17:57:31 -04:00
|
|
|
Args: cli.NoArgs,
|
2016-04-19 12:59:48 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runCreate(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.StringVarP(&opts.driver, "driver", "d", "local", "Specify volume driver name")
|
|
|
|
flags.StringVar(&opts.name, "name", "", "Specify volume name")
|
|
|
|
flags.VarP(&opts.driverOpts, "opt", "o", "Set driver specific options")
|
|
|
|
flags.StringSliceVar(&opts.labels, "label", []string{}, "Set metadata for a volume")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCreate(dockerCli *client.DockerCli, opts createOptions) error {
|
|
|
|
client := dockerCli.Client()
|
|
|
|
|
|
|
|
volReq := types.VolumeCreateRequest{
|
|
|
|
Driver: opts.driver,
|
|
|
|
DriverOpts: opts.driverOpts.GetAll(),
|
|
|
|
Name: opts.name,
|
|
|
|
Labels: runconfigopts.ConvertKVStringsToMap(opts.labels),
|
|
|
|
}
|
|
|
|
|
|
|
|
vol, err := client.VolumeCreate(context.Background(), volReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s\n", vol.Name)
|
|
|
|
return nil
|
|
|
|
}
|