github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/image_tag.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/image" 23 24 "github.com/spf13/cobra" 25 ) 26 27 func newTagCommand() *cobra.Command { 28 var tagCommand = &cobra.Command{ 29 Use: "tag [flags] SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]", 30 Short: "Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE", 31 Args: IsExactArgs(2), 32 RunE: tagAction, 33 ValidArgsFunction: tagShellComplete, 34 SilenceUsage: true, 35 SilenceErrors: true, 36 } 37 return tagCommand 38 } 39 40 func tagAction(cmd *cobra.Command, args []string) error { 41 globalOptions, err := processRootCmdFlags(cmd) 42 if err != nil { 43 return err 44 } 45 46 options := types.ImageTagOptions{ 47 GOptions: globalOptions, 48 Source: args[0], 49 Target: args[1], 50 } 51 52 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address) 53 if err != nil { 54 return err 55 } 56 defer cancel() 57 58 return image.Tag(ctx, client, options) 59 } 60 61 func tagShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 62 if len(args) < 2 { 63 // show image names 64 return shellCompleteImageNames(cmd) 65 } 66 return nil, cobra.ShellCompDirectiveNoFileComp 67 }