github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/commands/image/tag.go (about) 1 package image 2 3 import ( 4 "fmt" 5 6 "github.com/docker/app/internal/image" 7 8 "github.com/docker/app/internal/store" 9 "github.com/docker/cli/cli" 10 "github.com/docker/cli/cli/config" 11 "github.com/pkg/errors" 12 "github.com/spf13/cobra" 13 ) 14 15 const tagExample = `- $ docker app image tag myapp myrepo/myapp:mytag 16 - $ docker app image tag myapp:tag myrepo/mynewapp:mytag 17 - $ docker app image tag 34be4a0c5f50 myrepo/mynewapp:mytag` 18 19 func tagCmd() *cobra.Command { 20 cmd := &cobra.Command{ 21 Short: "Create a new tag from an App image", 22 Use: "tag SOURCE_APP_IMAGE[:TAG] TARGET_APP_IMAGE[:TAG]", 23 Example: tagExample, 24 Args: cli.ExactArgs(2), 25 RunE: func(cmd *cobra.Command, args []string) error { 26 appstore, err := store.NewApplicationStore(config.Dir()) 27 if err != nil { 28 return err 29 } 30 31 imageStore, err := appstore.ImageStore() 32 if err != nil { 33 return err 34 } 35 36 return runTag(imageStore, args[0], args[1]) 37 }, 38 } 39 40 return cmd 41 } 42 43 func runTag(imageStore store.ImageStore, srcAppImage, destAppImage string) error { 44 srcRef, err := readBundle(srcAppImage, imageStore) 45 if err != nil { 46 return err 47 } 48 49 return storeBundle(srcRef, destAppImage, imageStore) 50 } 51 52 func readBundle(name string, imageStore store.ImageStore) (*image.AppImage, error) { 53 cnabRef, err := imageStore.LookUp(name) 54 if err != nil { 55 switch err.(type) { 56 case *store.UnknownReferenceError: 57 return nil, fmt.Errorf("could not tag %q: no such App image", name) 58 default: 59 return nil, errors.Wrapf(err, "could not tag %q", name) 60 } 61 62 } 63 64 bundle, err := imageStore.Read(cnabRef) 65 if err != nil { 66 return nil, errors.Wrapf(err, "could not tag %q: no such App image", name) 67 } 68 return bundle, nil 69 } 70 71 func storeBundle(bundle *image.AppImage, name string, imageStore store.ImageStore) error { 72 cnabRef, err := store.StringToNamedRef(name) 73 if err != nil { 74 return err 75 } 76 _, err = imageStore.Store(bundle, cnabRef) 77 return err 78 }