1- package commands
1+ package build
22
33import (
44 "bytes"
55 "context"
66 "encoding/json"
77 "errors"
88 "fmt"
9- "io"
109 "io/ioutil"
1110 "os"
12- "path"
1311 "strconv"
1412 "strings"
1513
@@ -23,13 +21,11 @@ import (
2321 "github.com/docker/buildx/util/progress"
2422 "github.com/docker/cli/cli"
2523 "github.com/docker/cli/cli/command"
26- "github.com/docker/cli/cli/compose/loader"
2724 "github.com/docker/distribution/reference"
2825 "github.com/moby/buildkit/client"
2926 "github.com/moby/buildkit/session"
3027 "github.com/moby/buildkit/session/auth/authprovider"
3128 "github.com/moby/buildkit/util/appcontext"
32- "github.com/opencontainers/go-digest"
3329 "github.com/sirupsen/logrus"
3430 "github.com/spf13/cobra"
3531)
@@ -42,7 +38,7 @@ type buildOptions struct {
4238 out string
4339}
4440
45- func buildCmd (dockerCli command.Cli ) * cobra.Command {
41+ func BuildCmd (dockerCli command.Cli ) * cobra.Command {
4642 var opts buildOptions
4743 cmd := & cobra.Command {
4844 Use : "build [APPLICATION]" ,
@@ -176,17 +172,6 @@ func runBuild(dockerCli command.Cli, application string, opt buildOptions) (refe
176172 return ref , nil
177173}
178174
179- func computeDigest (bundle io.WriterTo ) (reference.Named , error ) {
180- b := bytes.Buffer {}
181- _ , err := bundle .WriteTo (& b )
182- if err != nil {
183- return nil , err
184- }
185- digest := digest .SHA256 .FromBytes (b .Bytes ())
186- ref := sha {digest }
187- return ref , nil
188- }
189-
190175func createInvocationImageBuildOptions (dockerCli command.Cli , app * types.App ) (build.Options , error ) {
191176 buildContext := bytes .NewBuffer (nil )
192177 if err := packager .PackInvocationImageContext (dockerCli , app , buildContext ); err != nil {
@@ -233,95 +218,3 @@ func debugSolveResponses(resp map[string]*client.SolveResponse) {
233218 }
234219 }
235220}
236-
237- // parseCompose do parse app compose file and extract buildx Options
238- // We don't rely on bake's ReadTargets + TargetsToBuildOpt here as we have to skip environment variable interpolation
239- func parseCompose (app * types.App , options buildOptions ) (map [string ]build.Options , error ) {
240-
241- // Fixme can have > 1 composes ?
242- parsed , err := loader .ParseYAML (app .Composes ()[0 ])
243- if err != nil {
244- return nil , err
245- }
246-
247- services , ok := parsed ["services" ].(map [string ]interface {})
248- if ! ok {
249- return nil , fmt .Errorf ("Invalid compose file: 'services' should be a map" )
250- }
251-
252- opts := map [string ]build.Options {}
253- for name , cfg := range services {
254- config , ok := cfg .(map [string ]interface {})
255- if ! ok {
256- return nil , fmt .Errorf ("Invalid compose file: service %s isn't a map" , name )
257- }
258- bc , ok := config ["build" ]
259- if ! ok {
260- continue
261- }
262- var buildContext string
263- dockerfilePath := "Dockerfile"
264- var buildargs map [string ]string
265- switch bc .(type ) {
266- case string :
267- buildContext = bc .(string )
268- case map [string ]interface {}:
269- buildconfig := bc .(map [string ]interface {})
270- buildContext = buildconfig ["context" ].(string )
271- if dockerfile , ok := buildconfig ["dockerfile" ]; ok {
272- dockerfilePath = dockerfile .(string )
273- }
274- if a , ok := buildconfig ["args" ]; ok {
275- switch a .(type ) {
276- case map [string ]interface {}:
277- buildargs = make (map [string ]string )
278- for k , v := range a .(map [string ]interface {}) {
279- buildargs [k ] = v .(string )
280- }
281- // FIXME also support the list-style syntax
282- default :
283- return nil , fmt .Errorf ("Invalid compose file: service %s build args is invalid" , name )
284- }
285- }
286- default :
287- return nil , fmt .Errorf ("Invalid compose file: service %s build is invalid" , name )
288- }
289-
290- // FIXME the compose file we build from x.dockerapp refers to docker context in parent folder.
291- // Maybe docker app init should update such relative paths accordingly ?
292- buildContext = path .Join (app .Path , ".." , buildContext )
293- dockerfilePath = path .Join (buildContext , dockerfilePath )
294- opts [name ] = build.Options {
295- Inputs : build.Inputs {
296- ContextPath : buildContext ,
297- DockerfilePath : dockerfilePath ,
298- },
299- BuildArgs : buildargs ,
300- NoCache : options .noCache ,
301- Pull : options .pull ,
302- }
303- }
304- return opts , nil
305- }
306-
307- type sha struct {
308- d digest.Digest
309- }
310-
311- var _ reference.Named = sha {"" }
312- var _ reference.Digested = sha {"" }
313-
314- // Digest implement Digested.Digest()
315- func (s sha ) Digest () digest.Digest {
316- return s .d
317- }
318-
319- // Digest implement Named.String()
320- func (s sha ) String () string {
321- return s .d .String ()
322- }
323-
324- // Digest implement Named.Name()
325- func (s sha ) Name () string {
326- return s .d .String ()
327- }
0 commit comments