summaryrefslogtreecommitdiff
path: root/cmd/podman/volume_create.go
blob: 0b5f8d1e3df90a9f14792eddd7f904411b14ca40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main

import (
	"fmt"

	"github.com/containers/libpod/cmd/podman/libpodruntime"
	"github.com/containers/libpod/libpod"
	"github.com/pkg/errors"
	"github.com/urfave/cli"
)

var volumeCreateDescription = `
podman volume create

Creates a new volume. If using the default driver, "local", the volume will
be created at.`

var volumeCreateFlags = []cli.Flag{
	cli.StringFlag{
		Name:  "driver",
		Usage: "Specify volume driver name (default local)",
	},
	cli.StringSliceFlag{
		Name:  "label, l",
		Usage: "Set metadata for a volume (default [])",
	},
	cli.StringSliceFlag{
		Name:  "opt, o",
		Usage: "Set driver specific options (default [])",
	},
}

var volumeCreateCommand = cli.Command{
	Name:                   "create",
	Usage:                  "Create a new volume",
	Description:            volumeCreateDescription,
	Flags:                  volumeCreateFlags,
	Action:                 volumeCreateCmd,
	SkipArgReorder:         true,
	ArgsUsage:              "[VOLUME-NAME]",
	UseShortOptionHandling: true,
}

func volumeCreateCmd(c *cli.Context) error {
	var (
		options []libpod.VolumeCreateOption
		err     error
		volName string
	)

	if err = validateFlags(c, volumeCreateFlags); err != nil {
		return err
	}

	runtime, err := libpodruntime.GetRuntime(c)
	if err != nil {
		return errors.Wrapf(err, "error creating libpod runtime")
	}
	defer runtime.Shutdown(false)

	if len(c.Args()) > 1 {
		return errors.Errorf("too many arguments, create takes at most 1 argument")
	}

	if len(c.Args()) > 0 {
		volName = c.Args()[0]
		options = append(options, libpod.WithVolumeName(volName))
	}

	if c.IsSet("driver") {
		options = append(options, libpod.WithVolumeDriver(c.String("driver")))
	}

	labels, err := getAllLabels([]string{}, c.StringSlice("label"))
	if err != nil {
		return errors.Wrapf(err, "unable to process labels")
	}
	if len(labels) != 0 {
		options = append(options, libpod.WithVolumeLabels(labels))
	}

	opts, err := getAllLabels([]string{}, c.StringSlice("opt"))
	if err != nil {
		return errors.Wrapf(err, "unable to process options")
	}
	if len(options) != 0 {
		options = append(options, libpod.WithVolumeOptions(opts))
	}

	vol, err := runtime.NewVolume(getContext(), options...)
	if err != nil {
		return err
	}
	fmt.Printf("%s\n", vol.Name())

	return nil
}