1717package registry
1818
1919import (
20+ "context"
2021 "os"
2122 "path/filepath"
2223 "strings"
2324
25+ "github.com/atomist-skills/go-skill"
2426 "github.com/docker/docker/client"
2527 "github.com/google/go-containerregistry/pkg/authn"
2628 "github.com/google/go-containerregistry/pkg/name"
@@ -76,7 +78,11 @@ func SaveImage(image string, client client.APIClient) (v1.Image, string, error)
7678 if err != nil {
7779 return nil , "" , errors .Wrapf (err , "failed to pull image: %s" , image )
7880 } else {
79- path , err = saveOci (img , ref , path )
81+ im , _ , err := client .ImageInspectWithRaw (context .Background (), image )
82+ if err != nil {
83+ return nil , "" , errors .Wrapf (err , "failed to get local image: %s" , image )
84+ }
85+ path , err = saveOci (im .ID , img , ref , path )
8086 if err != nil {
8187 return nil , "" , errors .Wrapf (err , "failed to save image: %s" , image )
8288 }
@@ -87,7 +93,15 @@ func SaveImage(image string, client client.APIClient) (v1.Image, string, error)
8793 if err != nil {
8894 return nil , "" , errors .Wrapf (err , "failed to pull image: %s" , image )
8995 }
90- path , err = saveOci (img , ref , path )
96+ var digest string
97+ identifier := ref .Identifier ()
98+ if strings .HasPrefix (identifier , "sha256:" ) {
99+ digest = identifier
100+ } else {
101+ digestHash , _ := img .Digest ()
102+ digest = digestHash .String ()
103+ }
104+ path , err = saveOci (digest , img , ref , path )
91105 if err != nil {
92106 return nil , "" , errors .Wrapf (err , "failed to save image: %s" , image )
93107 }
@@ -97,16 +111,10 @@ func SaveImage(image string, client client.APIClient) (v1.Image, string, error)
97111
98112// saveOci writes the v1.Image img as an OCI Image Layout at path. If a layout
99113// already exists at that path, it will add the image to the index.
100- func saveOci (img v1.Image , ref name.Reference , path string ) (string , error ) {
101- var digest string
102- identifier := ref .Identifier ()
103- if strings .HasPrefix (identifier , "sha256:" ) {
104- digest = identifier
105- } else {
106- digestHash , _ := img .Digest ()
107- digest = digestHash .String ()
108- }
114+ func saveOci (digest string , img v1.Image , ref name.Reference , path string ) (string , error ) {
109115 finalPath := strings .Replace (filepath .Join (path , digest ), ":" , string (os .PathSeparator ), 1 )
116+ skill .Log .Debugf ("Copying image to %s" , finalPath )
117+
110118 if _ , err := os .Stat (finalPath ); ! os .IsNotExist (err ) {
111119 return finalPath , nil
112120 }
0 commit comments