4
4
"archive/tar"
5
5
"bytes"
6
6
"context"
7
+ "path/filepath"
7
8
"regexp"
8
9
"strconv"
9
10
@@ -72,12 +73,11 @@ func (bc *Client) initContext(ctx context.Context) (*buildContext, error) {
72
73
if v , err := strconv .ParseBool (opts [keyContextKeepGitDirArg ]); err == nil {
73
74
keepGit = v
74
75
}
75
- if st , ok := detectGitContext (opts [localNameContext ], keepGit ); ok {
76
+ if st , ok := DetectGitContext (opts [localNameContext ], keepGit ); ok {
76
77
bctx .context = st
77
78
bctx .dockerfile = st
78
- } else if httpPrefix .MatchString (opts [localNameContext ]) {
79
- httpContext := llb .HTTP (opts [localNameContext ], llb .Filename ("context" ), WithInternalName ("load remote build context" ))
80
- def , err := httpContext .Marshal (ctx , bc .marshalOpts ()... )
79
+ } else if st , filename , ok := DetectHTTPContext (opts [localNameContext ]); ok {
80
+ def , err := st .Marshal (ctx , bc .marshalOpts ()... )
81
81
if err != nil {
82
82
return nil , errors .Wrapf (err , "failed to marshal httpcontext" )
83
83
}
@@ -94,7 +94,7 @@ func (bc *Client) initContext(ctx context.Context) (*buildContext, error) {
94
94
}
95
95
96
96
dt , err := ref .ReadFile (ctx , client.ReadRequest {
97
- Filename : "context" ,
97
+ Filename : filename ,
98
98
Range : & client.FileRange {
99
99
Length : 1024 ,
100
100
},
@@ -103,13 +103,13 @@ func (bc *Client) initContext(ctx context.Context) (*buildContext, error) {
103
103
return nil , errors .Wrapf (err , "failed to read downloaded context" )
104
104
}
105
105
if isArchive (dt ) {
106
- bc := llb .Scratch ().File (llb .Copy (httpContext , "/context" , "/" , & llb.CopyInfo {
106
+ bc := llb .Scratch ().File (llb .Copy (* st , filepath . Join ( "/" , filename ) , "/" , & llb.CopyInfo {
107
107
AttemptUnpack : true ,
108
108
}))
109
109
bctx .context = & bc
110
110
} else {
111
- bctx .filename = "context"
112
- bctx .context = & httpContext
111
+ bctx .filename = filename
112
+ bctx .context = st
113
113
}
114
114
bctx .dockerfile = bctx .context
115
115
} else if (& gwcaps ).Supports (gwpb .CapFrontendInputs ) == nil {
@@ -140,7 +140,7 @@ func (bc *Client) initContext(ctx context.Context) (*buildContext, error) {
140
140
return bctx , nil
141
141
}
142
142
143
- func detectGitContext (ref string , keepGit bool ) (* llb.State , bool ) {
143
+ func DetectGitContext (ref string , keepGit bool ) (* llb.State , bool ) {
144
144
g , err := gitutil .ParseGitRef (ref )
145
145
if err != nil {
146
146
return nil , false
@@ -158,6 +158,15 @@ func detectGitContext(ref string, keepGit bool) (*llb.State, bool) {
158
158
return & st , true
159
159
}
160
160
161
+ func DetectHTTPContext (ref string ) (* llb.State , string , bool ) {
162
+ filename := "context"
163
+ if httpPrefix .MatchString (ref ) {
164
+ st := llb .HTTP (ref , llb .Filename (filename ), WithInternalName ("load remote build context" ))
165
+ return & st , filename , true
166
+ }
167
+ return nil , "" , false
168
+ }
169
+
161
170
func isArchive (header []byte ) bool {
162
171
for _ , m := range [][]byte {
163
172
{0x42 , 0x5A , 0x68 }, // bzip2
0 commit comments