@@ -3,6 +3,7 @@ package image
33import (
44 "context"
55 "io"
6+ "strings"
67
78 "github.com/containers/image/v5/image"
89 "github.com/containers/image/v5/pkg/blobinfocache"
@@ -53,6 +54,11 @@ func NewImage(ctx context.Context, image Reference, transports []string, option
5354 var domain string
5455 var auth * imageTypes.DockerAuthConfig
5556
57+ osChoice , archChoice , variantChoice , err := parsePlatform (option .Platform )
58+ if err != nil {
59+ return RealImage {}, err
60+ }
61+
5662 originalName := image .Name
5763 if ! image .IsFile {
5864 named , err := reference .ParseNormalizedNamed (image .Name )
@@ -70,8 +76,9 @@ func NewImage(ctx context.Context, image Reference, transports []string, option
7076 }
7177
7278 sys := & imageTypes.SystemContext {
73- // TODO: make OSChoice configurable
74- OSChoice : "linux" ,
79+ OSChoice : osChoice ,
80+ ArchitectureChoice : archChoice ,
81+ VariantChoice : variantChoice ,
7582 DockerAuthConfig : auth ,
7683 DockerDisableV1Ping : option .SkipPing ,
7784 DockerInsecureSkipTLSVerify : imageTypes .NewOptionalBool (option .InsecureSkipTLSVerify ),
@@ -94,6 +101,31 @@ func NewImage(ctx context.Context, image Reference, transports []string, option
94101 }, nil
95102}
96103
104+ func parsePlatform (p string ) (osChoice , archChoice , variantChoice string , err error ) {
105+ osChoice = "linux"
106+ if p == "" {
107+ return osChoice , "" , "" , nil
108+ }
109+ parts := strings .Split (p , "/" )
110+ switch len (parts ) {
111+ case 1 :
112+ archChoice = parts [0 ]
113+ case 2 :
114+ osChoice = parts [0 ]
115+ archChoice = parts [1 ]
116+ case 3 :
117+ osChoice = parts [0 ]
118+ archChoice = parts [1 ]
119+ variantChoice = parts [2 ]
120+ default :
121+ return "" , "" , "" , xerrors .Errorf ("invalid platform %q" , p )
122+ }
123+ if osChoice == "" || archChoice == "" {
124+ return "" , "" , "" , xerrors .Errorf ("invalid platform %q" , p )
125+ }
126+ return osChoice , archChoice , variantChoice , nil
127+ }
128+
97129func newSource (ctx context.Context , imageName string , transports []string , sys * imageTypes.SystemContext ) (
98130 ImageSource , ImageCloser , error ) {
99131 err := xerrors .New ("no valid transport" )
0 commit comments