Skip to content

Commit f17630e

Browse files
committed
dockerui: expose context detection functions as public
Buildx uses these functions, so it makes sense to expose these so that we don't re-implement them and cause drift over time (which we have already done). Signed-off-by: Justin Chadwell <[email protected]>
1 parent 3187d2d commit f17630e

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

frontend/dockerui/context.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"archive/tar"
55
"bytes"
66
"context"
7+
"path/filepath"
78
"regexp"
89
"strconv"
910

@@ -72,12 +73,11 @@ func (bc *Client) initContext(ctx context.Context) (*buildContext, error) {
7273
if v, err := strconv.ParseBool(opts[keyContextKeepGitDirArg]); err == nil {
7374
keepGit = v
7475
}
75-
if st, ok := detectGitContext(opts[localNameContext], keepGit); ok {
76+
if st, ok := DetectGitContext(opts[localNameContext], keepGit); ok {
7677
bctx.context = st
7778
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()...)
8181
if err != nil {
8282
return nil, errors.Wrapf(err, "failed to marshal httpcontext")
8383
}
@@ -94,7 +94,7 @@ func (bc *Client) initContext(ctx context.Context) (*buildContext, error) {
9494
}
9595

9696
dt, err := ref.ReadFile(ctx, client.ReadRequest{
97-
Filename: "context",
97+
Filename: filename,
9898
Range: &client.FileRange{
9999
Length: 1024,
100100
},
@@ -103,13 +103,13 @@ func (bc *Client) initContext(ctx context.Context) (*buildContext, error) {
103103
return nil, errors.Wrapf(err, "failed to read downloaded context")
104104
}
105105
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{
107107
AttemptUnpack: true,
108108
}))
109109
bctx.context = &bc
110110
} else {
111-
bctx.filename = "context"
112-
bctx.context = &httpContext
111+
bctx.filename = filename
112+
bctx.context = st
113113
}
114114
bctx.dockerfile = bctx.context
115115
} else if (&gwcaps).Supports(gwpb.CapFrontendInputs) == nil {
@@ -140,7 +140,7 @@ func (bc *Client) initContext(ctx context.Context) (*buildContext, error) {
140140
return bctx, nil
141141
}
142142

143-
func detectGitContext(ref string, keepGit bool) (*llb.State, bool) {
143+
func DetectGitContext(ref string, keepGit bool) (*llb.State, bool) {
144144
g, err := gitutil.ParseGitRef(ref)
145145
if err != nil {
146146
return nil, false
@@ -158,6 +158,15 @@ func detectGitContext(ref string, keepGit bool) (*llb.State, bool) {
158158
return &st, true
159159
}
160160

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+
161170
func isArchive(header []byte) bool {
162171
for _, m := range [][]byte{
163172
{0x42, 0x5A, 0x68}, // bzip2

frontend/dockerui/namedcontext.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ func (bc *Client) namedContext(ctx context.Context, name string, nameWithPlatfor
8181
}
8282
return &st, &img, nil
8383
case "git":
84-
st, ok := detectGitContext(v, true)
84+
st, ok := DetectGitContext(v, true)
8585
if !ok {
8686
return nil, nil, errors.Errorf("invalid git context %s", v)
8787
}
8888
return st, nil, nil
8989
case "http", "https":
90-
st, ok := detectGitContext(v, true)
90+
st, ok := DetectGitContext(v, true)
9191
if !ok {
9292
httpst := llb.HTTP(v, llb.WithCustomName("[context "+nameWithPlatform+"] "+v))
9393
st = &httpst

0 commit comments

Comments
 (0)