github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/volumes/create.go (about)

     1  package volumes
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/containers/libpod/cmd/podmanV2/parse"
     8  	"github.com/containers/libpod/cmd/podmanV2/registry"
     9  	"github.com/containers/libpod/pkg/domain/entities"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var (
    15  	createDescription = `If using the default driver, "local", the volume will be created on the host in the volumes directory under container storage.`
    16  
    17  	createCommand = &cobra.Command{
    18  		Use:   "create [flags] [NAME]",
    19  		Short: "Create a new volume",
    20  		Long:  createDescription,
    21  		RunE:  create,
    22  		Example: `podman volume create myvol
    23    podman volume create
    24    podman volume create --label foo=bar myvol`,
    25  	}
    26  )
    27  
    28  var (
    29  	createOpts = entities.VolumeCreateOptions{}
    30  	opts       = struct {
    31  		Label []string
    32  		Opts  []string
    33  	}{}
    34  )
    35  
    36  func init() {
    37  	registry.Commands = append(registry.Commands, registry.CliCommand{
    38  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    39  		Command: createCommand,
    40  		Parent:  volumeCmd,
    41  	})
    42  	flags := createCommand.Flags()
    43  	flags.StringVar(&createOpts.Driver, "driver", "", "Specify volume driver name (default local)")
    44  	flags.StringSliceVarP(&opts.Label, "label", "l", []string{}, "Set metadata for a volume (default [])")
    45  	flags.StringArrayVarP(&opts.Opts, "opt", "o", []string{}, "Set driver specific options (default [])")
    46  }
    47  
    48  func create(cmd *cobra.Command, args []string) error {
    49  	var (
    50  		err error
    51  	)
    52  	if len(args) > 1 {
    53  		return errors.Errorf("too many arguments, create takes at most 1 argument")
    54  	}
    55  	if len(args) > 0 {
    56  		createOpts.Name = args[0]
    57  	}
    58  	createOpts.Label, err = parse.GetAllLabels([]string{}, opts.Label)
    59  	if err != nil {
    60  		return errors.Wrapf(err, "unable to process labels")
    61  	}
    62  	createOpts.Options, err = parse.GetAllLabels([]string{}, opts.Opts)
    63  	if err != nil {
    64  		return errors.Wrapf(err, "unable to process options")
    65  	}
    66  	response, err := registry.ContainerEngine().VolumeCreate(context.Background(), createOpts)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	fmt.Println(response.IdOrName)
    71  	return nil
    72  }