소스 검색

introduce `compose build` command

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 4 년 전
부모
커밋
5952183eca
9개의 변경된 파일108개의 추가작업 그리고 0개의 파일을 삭제
  1. 4 0
      aci/compose.go
  2. 4 0
      api/client/compose.go
  3. 2 0
      api/compose/api.go
  4. 67 0
      cli/cmd/compose/build.go
  5. 5 0
      cli/cmd/compose/compose.go
  6. 4 0
      ecs/local/compose.go
  7. 5 0
      ecs/up.go
  8. 5 0
      example/backend.go
  9. 12 0
      local/compose.go

+ 4 - 0
aci/compose.go

@@ -44,6 +44,10 @@ func newComposeService(ctx store.AciContext) aciComposeService {
 	}
 }
 
+func (cs *aciComposeService) Build(ctx context.Context, project *types.Project) error {
+	return errdefs.ErrNotImplemented
+}
+
 func (cs *aciComposeService) Up(ctx context.Context, project *types.Project, detach bool) error {
 	logrus.Debugf("Up on project with name %q", project.Name)
 

+ 4 - 0
api/client/compose.go

@@ -29,6 +29,10 @@ import (
 type composeService struct {
 }
 
+func (c *composeService) Build(ctx context.Context, project *types.Project) error {
+	return errdefs.ErrNotImplemented
+}
+
 // Up executes the equivalent to a `compose up`
 func (c *composeService) Up(context.Context, *types.Project, bool) error {
 	return errdefs.ErrNotImplemented

+ 2 - 0
api/compose/api.go

@@ -25,6 +25,8 @@ import (
 
 // Service manages a compose project
 type Service interface {
+	// Build executes the equivalent to a `compose build`
+	Build(ctx context.Context, project *types.Project) error
 	// Up executes the equivalent to a `compose up`
 	Up(ctx context.Context, project *types.Project, detach bool) error
 	// Down executes the equivalent to a `compose down`

+ 67 - 0
cli/cmd/compose/build.go

@@ -0,0 +1,67 @@
+/*
+   Copyright 2020 Docker Compose CLI authors
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+package compose
+
+import (
+	"context"
+
+	"github.com/compose-spec/compose-go/cli"
+	"github.com/spf13/cobra"
+
+	"github.com/docker/compose-cli/api/client"
+	"github.com/docker/compose-cli/progress"
+)
+
+type buildOptions struct {
+	composeOptions
+}
+
+func buildCommand() *cobra.Command {
+	opts := buildOptions{}
+	buildCmd := &cobra.Command{
+		Use: "build [SERVICE...]",
+		RunE: func(cmd *cobra.Command, args []string) error {
+			return runBuild(cmd.Context(), opts, args)
+		},
+	}
+	return buildCmd
+}
+
+func runBuild(ctx context.Context, opts buildOptions, services []string) error {
+	c, err := client.New(ctx)
+	if err != nil {
+		return err
+	}
+
+	_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
+		options, err := opts.toProjectOptions()
+		if err != nil {
+			return "", err
+		}
+		project, err := cli.ProjectFromOptions(options)
+		if err != nil {
+			return "", err
+		}
+
+		err = filter(project, services)
+		if err != nil {
+			return "", err
+		}
+		return "", c.ComposeService().Build(ctx, project)
+	})
+	return err
+}

+ 5 - 0
cli/cmd/compose/compose.go

@@ -25,6 +25,7 @@ import (
 	"github.com/spf13/pflag"
 
 	"github.com/docker/compose-cli/api/client"
+	"github.com/docker/compose-cli/context/store"
 	"github.com/docker/compose-cli/errdefs"
 )
 
@@ -89,6 +90,10 @@ func Command(contextType string) *cobra.Command {
 		convertCommand(),
 	)
 
+	if contextType == store.LocalContextType {
+		command.AddCommand(buildCommand())
+	}
+
 	return command
 }
 

+ 4 - 0
ecs/local/compose.go

@@ -41,6 +41,10 @@ import (
 	"golang.org/x/mod/semver"
 )
 
+func (e ecsLocalSimulation) Build(ctx context.Context, project *types.Project) error {
+	return errdefs.ErrNotImplemented
+}
+
 func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, detach bool) error {
 	cmd := exec.Command("docker-compose", "version", "--short")
 	b := bytes.Buffer{}

+ 5 - 0
ecs/up.go

@@ -24,8 +24,13 @@ import (
 	"syscall"
 
 	"github.com/compose-spec/compose-go/types"
+	"github.com/docker/compose-cli/errdefs"
 )
 
+func (b *ecsAPIService) Build(ctx context.Context, project *types.Project) error {
+	return errdefs.ErrNotImplemented
+}
+
 func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, detach bool) error {
 	err := b.aws.CheckRequirements(ctx, b.Region)
 	if err != nil {

+ 5 - 0
example/backend.go

@@ -138,6 +138,11 @@ func (cs *containerService) Delete(ctx context.Context, id string, request conta
 
 type composeService struct{}
 
+func (cs *composeService) Build(ctx context.Context, project *types.Project) error {
+	fmt.Printf("Build command on project %q", project.Name)
+	return nil
+}
+
 func (cs *composeService) Up(ctx context.Context, project *types.Project, detach bool) error {
 	fmt.Printf("Up command on project %q", project.Name)
 	return nil

+ 12 - 0
local/compose.go

@@ -29,6 +29,7 @@ import (
 	"strings"
 
 	"github.com/compose-spec/compose-go/types"
+	"github.com/docker/buildx/build"
 	moby "github.com/docker/docker/api/types"
 	"github.com/docker/docker/api/types/container"
 	"github.com/docker/docker/api/types/filters"
@@ -53,6 +54,17 @@ type composeService struct {
 	apiClient *client.Client
 }
 
+func (s *composeService) Build(ctx context.Context, project *types.Project) error {
+	opts := map[string]build.Options{}
+	for _, service := range project.Services {
+		if service.Build != nil {
+			opts[service.Name] = s.toBuildOptions(service, project.WorkingDir)
+		}
+	}
+
+	return s.build(ctx, project, opts)
+}
+
 func (s *composeService) Up(ctx context.Context, project *types.Project, detach bool) error {
 	err := s.ensureImagesExists(ctx, project)
 	if err != nil {