44 "errors"
55 "fmt"
66 "net/http"
7+ "os"
78 "time"
89
910 "github.com/acorn-io/baaah/pkg/typed"
@@ -25,7 +26,8 @@ func NewImageCopy(c CommandContext) *cobra.Command {
2526 Use : `copy [flags] SOURCE DESTINATION
2627
2728 This command copies Acorn images between remote image registries.
28- It does not interact with images stored in the Acorn internal registry, or with the Acorn API in any way.` ,
29+ It does not interact with images stored in the Acorn internal registry, or with the Acorn API in any way.
30+ To set up credentials for a registry, use 'acorn login -l <registry>'. It only works with locally stored credentials.` ,
2931 Aliases : []string {"cp" },
3032 SilenceUsage : true ,
3133 Short : "Copy Acorn images between registries" ,
@@ -48,7 +50,21 @@ type ImageCopy struct {
4850 client ClientFactory
4951}
5052
51- func (a * ImageCopy ) Run (cmd * cobra.Command , args []string ) error {
53+ func (a * ImageCopy ) Run (cmd * cobra.Command , args []string ) (err error ) {
54+ // Print a helpful error message for the user if they end up getting an authentication error
55+ defer func () {
56+ if err != nil {
57+ var terr * transport.Error
58+ if ok := errors .As (err , & terr ); ok {
59+ if terr .StatusCode == http .StatusForbidden {
60+ _ , _ = fmt .Fprintln (os .Stderr , "Registry authentication failed. Try running 'acorn login -l <registry>'" )
61+ } else if terr .StatusCode == http .StatusUnauthorized {
62+ _ , _ = fmt .Fprintln (os .Stderr , "Registry authorization failed. Ensure that you have the correct permissions to push to this registry. Run 'acorn login -l <registry>' if you have not logged in yet." )
63+ }
64+ }
65+ }
66+ }()
67+
5268 source , err := name .ParseReference (args [0 ], name .WithDefaultRegistry (images .NoDefaultRegistry ))
5369 if err != nil {
5470 return err
@@ -248,13 +264,23 @@ func (a *ImageCopy) copyRepo(args []string, sourceOpts, destOpts []remote.Option
248264}
249265
250266func (a * ImageCopy ) copyTag (source name.Reference , newTag string , sourceOpts []remote.Option ) error {
267+ // -a is not supported for this operation, so check if it is set and return an error if so
268+ if a .AllTags {
269+ return errors .New ("cannot use --all-tags with a tag destination" )
270+ }
271+
251272 sourceIndex , err := remote .Index (source , sourceOpts ... )
252273 if err != nil {
253274 return err
254275 }
255276
256277 dest := source .Context ().Tag (newTag )
257278
279+ // Parse it again to make sure that the tag provided by the user is valid
280+ if _ , err := name .ParseReference (dest .String ()); err != nil {
281+ return err
282+ }
283+
258284 if ! a .Force {
259285 if err := errIfImageExistsAndIsDifferent (sourceIndex , dest , sourceOpts ); err != nil {
260286 return err
0 commit comments