diff --git a/cmd/nerdctl/image_tag.go b/cmd/nerdctl/image_tag.go new file mode 100644 index 00000000000..f8000d887cb --- /dev/null +++ b/cmd/nerdctl/image_tag.go @@ -0,0 +1,57 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package main + +import ( + "github.com/containerd/nerdctl/pkg/api/types" + "github.com/containerd/nerdctl/pkg/cmd/image" + + "github.com/spf13/cobra" +) + +func newTagCommand() *cobra.Command { + var tagCommand = &cobra.Command{ + Use: "tag [flags] SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]", + Short: "Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE", + Args: IsExactArgs(2), + RunE: tagAction, + ValidArgsFunction: tagShellComplete, + SilenceUsage: true, + SilenceErrors: true, + } + return tagCommand +} + +func tagAction(cmd *cobra.Command, args []string) error { + globalOptions, err := processRootCmdFlags(cmd) + if err != nil { + return err + } + return image.Tag(cmd.Context(), types.ImageTagCommandOptions{ + GOptions: globalOptions, + Source: args[0], + Target: args[1], + }) +} + +func tagShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + if len(args) < 2 { + // show image names + return shellCompleteImageNames(cmd) + } + return nil, cobra.ShellCompDirectiveNoFileComp +} diff --git a/pkg/api/types/image_types.go b/pkg/api/types/image_types.go index d130f8e8d49..bdc0620eed1 100644 --- a/pkg/api/types/image_types.go +++ b/pkg/api/types/image_types.go @@ -157,3 +157,12 @@ type ImagePushCommandOptions struct { // AllowNondistributableArtifacts allow pushing non-distributable artifacts AllowNondistributableArtifacts bool } + +type ImageTagCommandOptions struct { + // GOptions is the global options + GOptions GlobalCommandOptions + // Source is the image to be referenced. + Source string + // Target is the image to be created. + Target string +} diff --git a/cmd/nerdctl/tag.go b/pkg/cmd/image/tag.go similarity index 61% rename from cmd/nerdctl/tag.go rename to pkg/cmd/image/tag.go index c1e496328ed..f68778bb730 100644 --- a/cmd/nerdctl/tag.go +++ b/pkg/cmd/image/tag.go @@ -14,39 +14,21 @@ limitations under the License. */ -package main +package image import ( "context" "fmt" "github.com/containerd/containerd/errdefs" + "github.com/containerd/nerdctl/pkg/api/types" "github.com/containerd/nerdctl/pkg/clientutil" "github.com/containerd/nerdctl/pkg/idutil/imagewalker" "github.com/containerd/nerdctl/pkg/referenceutil" - - "github.com/spf13/cobra" ) -func newTagCommand() *cobra.Command { - var tagCommand = &cobra.Command{ - Use: "tag [flags] SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]", - Short: "Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE", - Args: IsExactArgs(2), - RunE: tagAction, - ValidArgsFunction: tagShellComplete, - SilenceUsage: true, - SilenceErrors: true, - } - return tagCommand -} - -func tagAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) - if err != nil { - return err - } - client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) +func Tag(ctx context.Context, options types.ImageTagCommandOptions) error { + client, ctx, cancel, err := clientutil.NewClient(ctx, options.GOptions.Namespace, options.GOptions.Address) if err != nil { return err } @@ -63,15 +45,15 @@ func tagAction(cmd *cobra.Command, args []string) error { return nil }, } - matchCount, err := imagewalker.Walk(ctx, args[0]) + matchCount, err := imagewalker.Walk(ctx, options.Source) if err != nil { return err } if matchCount < 1 { - return fmt.Errorf("%s: not found", args[0]) + return fmt.Errorf("%s: not found", options.Source) } - target, err := referenceutil.ParseDockerRef(args[1]) + target, err := referenceutil.ParseDockerRef(options.Target) if err != nil { return err } @@ -101,11 +83,3 @@ func tagAction(cmd *cobra.Command, args []string) error { } return nil } - -func tagShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - if len(args) < 2 { - // show image names - return shellCompleteImageNames(cmd) - } - return nil, cobra.ShellCompDirectiveNoFileComp -}