Browse Source

Remove command.DockerCli dependency

Signed-off-by: Ulysses Souza <[email protected]>
Ulysses Souza 4 years ago
parent
commit
0f3c214b48
2 changed files with 27 additions and 28 deletions
  1. 16 11
      pkg/compose/build.go
  2. 11 17
      pkg/compose/build_classic.go

+ 16 - 11
pkg/compose/build.go

@@ -29,8 +29,6 @@ import (
 	"github.com/docker/buildx/util/buildflags"
 	xprogress "github.com/docker/buildx/util/progress"
 	"github.com/docker/cli/cli/command"
-	"github.com/docker/cli/cli/flags"
-	"github.com/docker/docker/client"
 	bclient "github.com/moby/buildkit/client"
 	"github.com/moby/buildkit/session"
 	"github.com/moby/buildkit/session/auth/authprovider"
@@ -193,22 +191,29 @@ func (s *composeService) getLocalImagesDigests(ctx context.Context, project *typ
 	return images, nil
 }
 
+func (s *composeService) serverInfo(ctx context.Context) (command.ServerInfo, error) {
+	ping, err := s.apiClient.Ping(ctx)
+	if err != nil {
+		return command.ServerInfo{}, err
+	}
+	serverInfo := command.ServerInfo{
+		HasExperimental: ping.Experimental,
+		OSType:          ping.OSType,
+		BuildkitVersion: ping.BuilderVersion,
+	}
+	return serverInfo, err
+}
+
 func (s *composeService) doBuild(ctx context.Context, project *types.Project, opts map[string]build.Options, mode string) (map[string]string, error) {
 	if len(opts) == 0 {
 		return nil, nil
 	}
-	dockerCli, err := command.NewDockerCli()
-	if err != nil {
-		return nil, err
-	}
-	err = dockerCli.Initialize(flags.NewClientOptions(), command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) {
-		return s.apiClient, nil
-	}))
+	serverInfo, err := s.serverInfo(ctx)
 	if err != nil {
 		return nil, err
 	}
-	if buildkitEnabled, err := command.BuildKitEnabled(dockerCli.ServerInfo()); err != nil || !buildkitEnabled {
-		return s.doBuildClassic(ctx, dockerCli, opts)
+	if buildkitEnabled, err := command.BuildKitEnabled(serverInfo); err != nil || !buildkitEnabled {
+		return s.doBuildClassic(ctx, opts)
 	}
 	return s.doBuildBuildkit(ctx, project, opts, mode)
 }

+ 11 - 17
pkg/compose/build_classic.go

@@ -28,7 +28,6 @@ import (
 	"strings"
 
 	buildx "github.com/docker/buildx/build"
-	"github.com/docker/cli/cli/command"
 	"github.com/docker/cli/cli/command/image/build"
 	dockertypes "github.com/docker/docker/api/types"
 	"github.com/docker/docker/cli"
@@ -42,11 +41,11 @@ import (
 	"github.com/pkg/errors"
 )
 
-func (s *composeService) doBuildClassic(ctx context.Context, dockerCli *command.DockerCli, opts map[string]buildx.Options) (map[string]string, error) {
+func (s *composeService) doBuildClassic(ctx context.Context, opts map[string]buildx.Options) (map[string]string, error) {
 	var nameDigests = make(map[string]string)
 	var errs error
 	for name, o := range opts {
-		digest, err := doBuildClassicSimpleImage(ctx, dockerCli, o)
+		digest, err := s.doBuildClassicSimpleImage(ctx, o)
 		if err != nil {
 			errs = multierror.Append(errs, err).ErrorOrNil()
 		}
@@ -57,7 +56,7 @@ func (s *composeService) doBuildClassic(ctx context.Context, dockerCli *command.
 }
 
 // nolint: gocyclo
-func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli, options buildx.Options) (string, error) {
+func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options buildx.Options) (string, error) {
 	var (
 		buildCtx      io.ReadCloser
 		dockerfileCtx io.ReadCloser
@@ -70,8 +69,8 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli
 
 	dockerfileName := options.Inputs.DockerfilePath
 	specifiedContext := options.Inputs.ContextPath
-	progBuff := dockerCli.Out()
-	buildBuff := dockerCli.Out()
+	progBuff := os.Stdout
+	buildBuff := os.Stdout
 	if options.ImageIDFile != "" {
 		// Avoid leaving a stale file if we eventually fail
 		if err := os.Remove(options.ImageIDFile); err != nil && !os.IsNotExist(err) {
@@ -144,24 +143,19 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli
 		return "", err
 	}
 
-	// Setup an upload progress bar
-	progressOutput := streamformatter.NewProgressOutput(progBuff)
-	if !dockerCli.Out().IsTerminal() {
-		progressOutput = &lastProgressOutput{output: progressOutput}
-	}
-
 	// if up to this point nothing has set the context then we must have another
 	// way for sending it(streaming) and set the context to the Dockerfile
 	if dockerfileCtx != nil && buildCtx == nil {
 		buildCtx = dockerfileCtx
 	}
 
+	progressOutput := streamformatter.NewProgressOutput(progBuff)
 	var body io.Reader
 	if buildCtx != nil {
 		body = progress.NewProgressReader(buildCtx, progressOutput, 0, "", "Sending build context to Docker daemon")
 	}
 
-	configFile := dockerCli.ConfigFile()
+	configFile := s.configFile
 	creds, _ := configFile.GetAllCredentials()
 	authConfigs := make(map[string]dockertypes.AuthConfig, len(creds))
 	for k, auth := range creds {
@@ -174,7 +168,7 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli
 
 	ctx, cancel := context.WithCancel(ctx)
 	defer cancel()
-	response, err := dockerCli.Client().ImageBuild(ctx, body, buildOptions)
+	response, err := s.apiClient.ImageBuild(ctx, body, buildOptions)
 	if err != nil {
 		return "", err
 	}
@@ -184,13 +178,13 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli
 	aux := func(msg jsonmessage.JSONMessage) {
 		var result dockertypes.BuildResult
 		if err := json.Unmarshal(*msg.Aux, &result); err != nil {
-			fmt.Fprintf(dockerCli.Err(), "Failed to parse aux message: %s", err)
+			fmt.Fprintf(os.Stderr, "Failed to parse aux message: %s", err)
 		} else {
 			imageID = result.ID
 		}
 	}
 
-	err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, dockerCli.Out().FD(), dockerCli.Out().IsTerminal(), aux)
+	err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, progBuff.Fd(), true, aux)
 	if err != nil {
 		if jerr, ok := err.(*jsonmessage.JSONError); ok {
 			// If no error code is set, default to 1
@@ -206,7 +200,7 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli
 	// daemon isn't running Windows.
 	if response.OSType != "windows" && runtime.GOOS == "windows" {
 		// if response.OSType != "windows" && runtime.GOOS == "windows" && !options.quiet {
-		fmt.Fprintln(dockerCli.Out(), "SECURITY WARNING: You are building a Docker "+
+		fmt.Fprintln(os.Stdout, "SECURITY WARNING: You are building a Docker "+
 			"image from Windows against a non-Windows Docker host. All files and "+
 			"directories added to build context will have '-rwxr-xr-x' permissions. "+
 			"It is recommended to double check and reset permissions for sensitive "+