Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions cmd/nerdctl/image_tag.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions pkg/api/types/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
40 changes: 7 additions & 33 deletions cmd/nerdctl/tag.go → pkg/cmd/image/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}