@@ -15,6 +15,7 @@ import (
15
15
"github.com/go-git/go-billy/v5"
16
16
"github.com/google/go-containerregistry/pkg/name"
17
17
"github.com/google/go-containerregistry/pkg/v1/remote"
18
+ "github.com/moby/buildkit/frontend/dockerfile/shell"
18
19
"github.com/tailscale/hujson"
19
20
)
20
21
@@ -317,30 +318,43 @@ func UserFromDockerfile(dockerfileContent string) string {
317
318
// ImageFromDockerfile inspects the contents of a provided Dockerfile
318
319
// and returns the image that will be used to run the container.
319
320
func ImageFromDockerfile (dockerfileContent string ) (name.Reference , error ) {
320
- args := map [string ]string {}
321
+ lexer := shell .NewLex ('\\' )
322
+ var args []string
321
323
var imageRef string
322
324
lines := strings .Split (dockerfileContent , "\n " )
323
325
// Iterate over lines in reverse
324
326
for i := len (lines ) - 1 ; i >= 0 ; i -- {
325
327
line := lines [i ]
326
- if strings .HasPrefix (line , "ARG " ) {
327
- arg : = strings .TrimSpace (strings . TrimPrefix ( line , "ARG " ) )
328
+ if arg , ok := strings .CutPrefix (line , "ARG " ); ok {
329
+ arg = strings .TrimSpace (arg )
328
330
if strings .Contains (arg , "=" ) {
329
331
parts := strings .SplitN (arg , "=" , 2 )
330
- args [parts [0 ]] = parts [1 ]
332
+ key , err := lexer .ProcessWord (parts [0 ], args )
333
+ if err != nil {
334
+ return nil , fmt .Errorf ("processing %q: %w" , line , err )
335
+ }
336
+ val , err := lexer .ProcessWord (parts [1 ], args )
337
+ if err != nil {
338
+ return nil , fmt .Errorf ("processing %q: %w" , line , err )
339
+ }
340
+ args = append (args , key + "=" + val )
331
341
}
332
342
continue
333
343
}
334
- if imageRef == "" && strings .HasPrefix (line , "FROM " ) {
335
- imageRef = strings .TrimPrefix (line , "FROM " )
344
+ if imageRef == "" {
345
+ if fromArgs , ok := strings .CutPrefix (line , "FROM " ); ok {
346
+ imageRef = fromArgs
347
+ }
336
348
}
337
349
}
338
350
if imageRef == "" {
339
351
return nil , fmt .Errorf ("no FROM directive found" )
340
352
}
341
- image , err := name .ParseReference (os .Expand (imageRef , func (s string ) string {
342
- return args [s ]
343
- }))
353
+ imageRef , err := lexer .ProcessWord (imageRef , args )
354
+ if err != nil {
355
+ return nil , fmt .Errorf ("processing %q: %w" , imageRef , err )
356
+ }
357
+ image , err := name .ParseReference (strings .TrimSpace (imageRef ))
344
358
if err != nil {
345
359
return nil , fmt .Errorf ("parse image ref %q: %w" , imageRef , err )
346
360
}
0 commit comments