github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/volume_create.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "github.com/containerd/nerdctl/pkg/api/types" 21 "github.com/containerd/nerdctl/pkg/cmd/volume" 22 23 "github.com/spf13/cobra" 24 ) 25 26 func newVolumeCreateCommand() *cobra.Command { 27 volumeCreateCommand := &cobra.Command{ 28 Use: "create [flags] [VOLUME]", 29 Short: "Create a volume", 30 Args: cobra.MaximumNArgs(1), 31 RunE: volumeCreateAction, 32 SilenceUsage: true, 33 SilenceErrors: true, 34 } 35 volumeCreateCommand.Flags().StringArray("label", nil, "Set a label on the volume") 36 return volumeCreateCommand 37 } 38 39 func processVolumeCreateOptions(cmd *cobra.Command) (types.VolumeCreateOptions, error) { 40 globalOptions, err := processRootCmdFlags(cmd) 41 if err != nil { 42 return types.VolumeCreateOptions{}, err 43 } 44 labels, err := cmd.Flags().GetStringArray("label") 45 if err != nil { 46 return types.VolumeCreateOptions{}, err 47 } 48 return types.VolumeCreateOptions{ 49 GOptions: globalOptions, 50 Labels: labels, 51 Stdout: cmd.OutOrStdout(), 52 }, nil 53 } 54 55 func volumeCreateAction(cmd *cobra.Command, args []string) error { 56 options, err := processVolumeCreateOptions(cmd) 57 if err != nil { 58 return err 59 } 60 volumeName := "" 61 if len(args) > 0 { 62 volumeName = args[0] 63 } 64 _, err = volume.Create(volumeName, options) 65 66 return err 67 }