chore: support multiple targets for docker image tag command

Signed-off-by: MohammadHasan Akbari <jarqvi.jarqvi@gmail.com>
This commit is contained in:
MohammadHasan Akbari 2024-11-08 19:23:09 +00:00
parent 8c22315e31
commit 8131e64543
5 changed files with 33 additions and 10 deletions

View File

@ -2,16 +2,20 @@ package image
import ( import (
"context" "context"
"fmt"
"strings"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion" "github.com/docker/cli/cli/command/completion"
"github.com/docker/docker/errdefs"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type tagOptions struct { type tagOptions struct {
image string image string
name string names []string
} }
// NewTagCommand creates a new `docker tag` command // NewTagCommand creates a new `docker tag` command
@ -19,12 +23,12 @@ func NewTagCommand(dockerCli command.Cli) *cobra.Command {
var opts tagOptions var opts tagOptions
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]", Use: "tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] [TARGET_IMAGE[:TAG]...]",
Short: "Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE", Short: "Create one or more tags TARGET_IMAGE that refers to SOURCE_IMAGE",
Args: cli.ExactArgs(2), Args: cli.RequiresMinArgs(2),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts.image = args[0] opts.image = args[0]
opts.name = args[1] opts.names = args[1:]
return runTag(cmd.Context(), dockerCli, opts) return runTag(cmd.Context(), dockerCli, opts)
}, },
Annotations: map[string]string{ 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 { 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
} }

View File

@ -56,7 +56,7 @@ The base command for the Docker CLI.
| [`stop`](stop.md) | Stop one or more running containers | | [`stop`](stop.md) | Stop one or more running containers |
| [`swarm`](swarm.md) | Manage Swarm | | [`swarm`](swarm.md) | Manage Swarm |
| [`system`](system.md) | Manage Docker | | [`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 | | [`top`](top.md) | Display the running processes of a container |
| [`trust`](trust.md) | Manage trust on Docker images | | [`trust`](trust.md) | Manage trust on Docker images |
| [`unpause`](unpause.md) | Unpause all processes within one or more containers | | [`unpause`](unpause.md) | Unpause all processes within one or more containers |

View File

@ -18,7 +18,7 @@ Manage images
| [`push`](image_push.md) | Upload an image to a registry | | [`push`](image_push.md) | Upload an image to a registry |
| [`rm`](image_rm.md) | Remove one or more images | | [`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) | | [`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 |

View File

@ -1,7 +1,7 @@
# tag # tag
<!---MARKER_GEN_START--> <!---MARKER_GEN_START-->
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE Create one or more tags TARGET_IMAGE that refers to SOURCE_IMAGE
### Aliases ### Aliases

View File

@ -1,7 +1,7 @@
# docker tag # docker tag
<!---MARKER_GEN_START--> <!---MARKER_GEN_START-->
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE Create one or more tags TARGET_IMAGE that refers to SOURCE_IMAGE
### Aliases ### Aliases