diff --git a/pkg/cli/copy.go b/pkg/cli/copy.go index 23adab608..0c522aa9a 100644 --- a/pkg/cli/copy.go +++ b/pkg/cli/copy.go @@ -56,17 +56,25 @@ func (a *ImageCopy) Run(cmd *cobra.Command, args []string) error { return err } + if !a.AllTags { + // Check if the source argument matches the name of a local image, and use it if it does. + // If there is an error, ignore it and move on, treating the argument as a remote image name. + if _, err := c.ImageGet(cmd.Context(), args[0]); err == nil { + return a.copyLocalToRemote(cmd, c, args, creds) + } + } + source, err := name.ParseReference(args[0]) if err != nil { return err } - dest, err := name.ParseReference(args[1]) + sourceAuth, _, err := creds.Get(cmd.Context(), source.Context().RegistryStr()) if err != nil { return err } - sourceAuth, _, err := creds.Get(cmd.Context(), source.Context().RegistryStr()) + dest, err := name.ParseReference(args[1]) if err != nil { return err } @@ -88,3 +96,25 @@ func (a *ImageCopy) Run(cmd *cobra.Command, args []string) error { return progressbar.Print(progress) } + +func (a *ImageCopy) copyLocalToRemote(cmd *cobra.Command, c client.Client, args []string, creds *credentials.Store) error { + dest, err := name.ParseReference(args[1]) + if err != nil { + return err + } + + destAuth, _, err := creds.Get(cmd.Context(), dest.Context().RegistryStr()) + if err != nil { + return err + } + + progress, err := c.ImageCopy(cmd.Context(), args[0], args[1], &client.ImageCopyOptions{ + Force: a.Force, + DestAuth: destAuth, + }) + if err != nil { + return err + } + + return progressbar.Print(progress) +} diff --git a/pkg/roles/roles.go b/pkg/roles/roles.go index dc1d8c32a..625c796e6 100644 --- a/pkg/roles/roles.go +++ b/pkg/roles/roles.go @@ -123,6 +123,7 @@ var ( Resources: []string{ "images/push", "images/pull", + "images/copy", "containerreplicas/exec", "secrets/reveal", },