From 8131e64543ee99786c8cd4583aa654d99c932d36 Mon Sep 17 00:00:00 2001 From: MohammadHasan Akbari Date: Fri, 8 Nov 2024 19:23:09 +0000 Subject: [PATCH] chore: support multiple targets for docker image tag command Signed-off-by: MohammadHasan Akbari --- cli/command/image/tag.go | 35 ++++++++++++++++++++----- docs/reference/commandline/docker.md | 2 +- docs/reference/commandline/image.md | 2 +- docs/reference/commandline/image_tag.md | 2 +- docs/reference/commandline/tag.md | 2 +- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/cli/command/image/tag.go b/cli/command/image/tag.go index 29da7f3753..6e1cd87f5b 100644 --- a/cli/command/image/tag.go +++ b/cli/command/image/tag.go @@ -2,16 +2,20 @@ package image import ( "context" + "fmt" + "strings" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" + "github.com/docker/docker/errdefs" + "github.com/pkg/errors" "github.com/spf13/cobra" ) type tagOptions struct { image string - name string + names []string } // NewTagCommand creates a new `docker tag` command @@ -19,12 +23,12 @@ func NewTagCommand(dockerCli command.Cli) *cobra.Command { var opts tagOptions cmd := &cobra.Command{ - Use: "tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]", - Short: "Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE", - Args: cli.ExactArgs(2), + Use: "tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] [TARGET_IMAGE[:TAG]...]", + Short: "Create one or more tags TARGET_IMAGE that refers to SOURCE_IMAGE", + Args: cli.RequiresMinArgs(2), RunE: func(cmd *cobra.Command, args []string) error { opts.image = args[0] - opts.name = args[1] + opts.names = args[1:] return runTag(cmd.Context(), dockerCli, opts) }, Annotations: map[string]string{ @@ -40,5 +44,24 @@ func NewTagCommand(dockerCli command.Cli) *cobra.Command { } func runTag(ctx context.Context, dockerCli command.Cli, opts tagOptions) error { - return dockerCli.Client().ImageTag(ctx, opts.image, opts.name) + var errs []string + fatalErr := false + for _, name := range opts.names { + err := dockerCli.Client().ImageTag(ctx, opts.image, name) + if err != nil { + if !errdefs.IsNotFound(err) { + fatalErr = true + } + errs = append(errs, err.Error()) + } + } + + if len(errs) > 0 { + msg := strings.Join(errs, "\n") + if fatalErr { + return errors.New(msg) + } + fmt.Fprintln(dockerCli.Err(), msg) + } + return nil } diff --git a/docs/reference/commandline/docker.md b/docs/reference/commandline/docker.md index bfa86b8b1d..f91b027da6 100644 --- a/docs/reference/commandline/docker.md +++ b/docs/reference/commandline/docker.md @@ -56,7 +56,7 @@ The base command for the Docker CLI. | [`stop`](stop.md) | Stop one or more running containers | | [`swarm`](swarm.md) | Manage Swarm | | [`system`](system.md) | Manage Docker | -| [`tag`](tag.md) | Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE | +| [`tag`](tag.md) | Create one or more tags TARGET_IMAGE that refers to SOURCE_IMAGE | | [`top`](top.md) | Display the running processes of a container | | [`trust`](trust.md) | Manage trust on Docker images | | [`unpause`](unpause.md) | Unpause all processes within one or more containers | diff --git a/docs/reference/commandline/image.md b/docs/reference/commandline/image.md index 3aa0843c24..7b0ed66d93 100644 --- a/docs/reference/commandline/image.md +++ b/docs/reference/commandline/image.md @@ -18,7 +18,7 @@ Manage images | [`push`](image_push.md) | Upload an image to a registry | | [`rm`](image_rm.md) | Remove one or more images | | [`save`](image_save.md) | Save one or more images to a tar archive (streamed to STDOUT by default) | -| [`tag`](image_tag.md) | Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE | +| [`tag`](image_tag.md) | Create one or more tags TARGET_IMAGE that refers to SOURCE_IMAGE | diff --git a/docs/reference/commandline/image_tag.md b/docs/reference/commandline/image_tag.md index ef0f8c29c9..1d3736700b 100644 --- a/docs/reference/commandline/image_tag.md +++ b/docs/reference/commandline/image_tag.md @@ -1,7 +1,7 @@ # tag -Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE +Create one or more tags TARGET_IMAGE that refers to SOURCE_IMAGE ### Aliases diff --git a/docs/reference/commandline/tag.md b/docs/reference/commandline/tag.md index 4e980570a1..b7e70df388 100644 --- a/docs/reference/commandline/tag.md +++ b/docs/reference/commandline/tag.md @@ -1,7 +1,7 @@ # docker tag -Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE +Create one or more tags TARGET_IMAGE that refers to SOURCE_IMAGE ### Aliases