github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_rename.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/container" 23 "github.com/spf13/cobra" 24 ) 25 26 func newRenameCommand() *cobra.Command { 27 var renameCommand = &cobra.Command{ 28 Use: "rename [flags] CONTAINER NEW_NAME", 29 Args: IsExactArgs(2), 30 Short: "rename a container", 31 RunE: renameAction, 32 ValidArgsFunction: renameShellComplete, 33 SilenceUsage: true, 34 SilenceErrors: true, 35 } 36 return renameCommand 37 } 38 39 func processContainerRenameOptions(cmd *cobra.Command) (types.ContainerRenameOptions, error) { 40 globalOptions, err := processRootCmdFlags(cmd) 41 if err != nil { 42 return types.ContainerRenameOptions{}, err 43 } 44 return types.ContainerRenameOptions{ 45 GOptions: globalOptions, 46 Stdout: cmd.OutOrStdout(), 47 }, nil 48 } 49 50 func renameAction(cmd *cobra.Command, args []string) error { 51 options, err := processContainerRenameOptions(cmd) 52 if err != nil { 53 return err 54 } 55 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address) 56 if err != nil { 57 return err 58 } 59 defer cancel() 60 return container.Rename(ctx, client, args[0], args[1], options) 61 } 62 func renameShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 63 return shellCompleteContainerNames(cmd, nil) 64 }