github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/namespace_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/clientutil" 22 "github.com/containerd/nerdctl/pkg/cmd/namespace" 23 "github.com/spf13/cobra" 24 ) 25 26 func newNamespaceCreateCommand() *cobra.Command { 27 namespaceCreateCommand := &cobra.Command{ 28 Use: "create NAMESPACE", 29 Short: "Create a new namespace", 30 Args: cobra.MinimumNArgs(1), 31 RunE: namespaceCreateAction, 32 SilenceUsage: true, 33 SilenceErrors: true, 34 } 35 36 namespaceCreateCommand.Flags().StringArrayP("label", "l", nil, "Set labels for a namespace") 37 return namespaceCreateCommand 38 } 39 40 func processNamespaceCreateCommandOption(cmd *cobra.Command) (types.NamespaceCreateOptions, error) { 41 globalOptions, err := processRootCmdFlags(cmd) 42 if err != nil { 43 return types.NamespaceCreateOptions{}, err 44 } 45 labels, err := cmd.Flags().GetStringArray("label") 46 if err != nil { 47 return types.NamespaceCreateOptions{}, err 48 } 49 return types.NamespaceCreateOptions{ 50 GOptions: globalOptions, 51 Labels: labels, 52 }, nil 53 } 54 55 func namespaceCreateAction(cmd *cobra.Command, args []string) error { 56 options, err := processNamespaceCreateCommandOption(cmd) 57 if err != nil { 58 return err 59 } 60 61 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address) 62 if err != nil { 63 return err 64 } 65 defer cancel() 66 67 return namespace.Create(ctx, client, args[0], options) 68 }