Browse Source

Pass reader Writer as options to backend, remove hardcoded os.Stdout

Signed-off-by: Guillaume Tardif <[email protected]>
Guillaume Tardif 5 years ago
parent
commit
32d5644937

+ 2 - 2
aci/compose.go

@@ -202,6 +202,6 @@ func (cs *aciComposeService) Convert(ctx context.Context, project *types.Project
 	return nil, errdefs.ErrNotImplemented
 }
 
-func (cs *aciComposeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
-	return "", errdefs.ErrNotImplemented
+func (cs *aciComposeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
+	return errdefs.ErrNotImplemented
 }

+ 2 - 2
api/client/compose.go

@@ -72,6 +72,6 @@ func (c *composeService) Convert(context.Context, *types.Project, compose.Conver
 	return nil, errdefs.ErrNotImplemented
 }
 
-func (c *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
-	return "", errdefs.ErrNotImplemented
+func (c *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
+	return errdefs.ErrNotImplemented
 }

+ 5 - 2
api/compose/api.go

@@ -18,6 +18,7 @@ package compose
 
 import (
 	"context"
+	"io"
 
 	"github.com/compose-spec/compose-go/types"
 )
@@ -47,7 +48,7 @@ type Service interface {
 	// Convert translate compose model into backend's native format
 	Convert(ctx context.Context, project *types.Project, options ConvertOptions) ([]byte, error)
 	// RunOneOffContainer creates a service oneoff container and starts its dependencies
-	RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (string, error)
+	RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) error
 }
 
 // UpOptions group options of the Up API
@@ -68,12 +69,14 @@ type ConvertOptions struct {
 	Format string
 }
 
-// RunOptions holds all flags for compose run
+// RunOptions options to execute compose run
 type RunOptions struct {
 	Name       string
 	Command    []string
 	Detach     bool
 	AutoRemove bool
+	Writer     io.Writer
+	Reader     io.Reader
 }
 
 // PortPublisher hold status about published port

+ 27 - 20
cli/cmd/compose/run.go

@@ -18,11 +18,12 @@ package compose
 
 import (
 	"context"
-	"fmt"
+	"os"
 
 	"github.com/compose-spec/compose-go/types"
 	"github.com/spf13/cobra"
 
+	"github.com/docker/compose-cli/api/client"
 	"github.com/docker/compose-cli/api/compose"
 	"github.com/docker/compose-cli/progress"
 )
@@ -74,20 +75,7 @@ func runRun(ctx context.Context, opts runOptions) error {
 
 	originalServices := project.Services
 	_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
-		dependencies := types.Services{}
-		for _, service := range originalServices {
-			if service.Name != opts.Name {
-				dependencies = append(dependencies, service)
-			}
-		}
-		project.Services = dependencies
-		if err := c.ComposeService().Create(ctx, project); err != nil {
-			return "", err
-		}
-		if err := c.ComposeService().Start(ctx, project, nil); err != nil {
-			return "", err
-		}
-		return "", nil
+		return "", startDependencies(ctx, c, project, opts.Name)
 	})
 	if err != nil {
 		return err
@@ -95,13 +83,32 @@ func runRun(ctx context.Context, opts runOptions) error {
 
 	project.Services = originalServices
 	// start container and attach to container streams
-	runOpts := compose.RunOptions{Name: opts.Name, Command: opts.Command, Detach: opts.Detach, AutoRemove: opts.Remove}
-	containerID, err := c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
-	if err != nil {
+	runOpts := compose.RunOptions{
+		Name:       opts.Name,
+		Command:    opts.Command,
+		Detach:     opts.Detach,
+		AutoRemove: opts.Remove,
+		Writer:     os.Stdout,
+		Reader:     os.Stdin,
+	}
+	return c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
+}
+
+func startDependencies(ctx context.Context, c *client.Client, project *types.Project, requestedService string) error {
+	originalServices := project.Services
+	dependencies := types.Services{}
+	for _, service := range originalServices {
+		if service.Name != requestedService {
+			dependencies = append(dependencies, service)
+		}
+	}
+	project.Services = dependencies
+	if err := c.ComposeService().Create(ctx, project); err != nil {
 		return err
 	}
-	if opts.Detach {
-		fmt.Printf("%s", containerID)
+	if err := c.ComposeService().Start(ctx, project, nil); err != nil {
+		return err
 	}
 	return nil
+
 }

+ 2 - 2
ecs/local/compose.go

@@ -163,6 +163,6 @@ func (e ecsLocalSimulation) Ps(ctx context.Context, projectName string) ([]compo
 func (e ecsLocalSimulation) List(ctx context.Context, projectName string) ([]compose.Stack, error) {
 	return e.compose.List(ctx, projectName)
 }
-func (e ecsLocalSimulation) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
-	return "", errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose run")
+func (e ecsLocalSimulation) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
+	return errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose run")
 }

+ 2 - 2
ecs/run.go

@@ -24,6 +24,6 @@ import (
 	"github.com/docker/compose-cli/errdefs"
 )
 
-func (b *ecsAPIService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
-	return "", errdefs.ErrNotImplemented
+func (b *ecsAPIService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
+	return errdefs.ErrNotImplemented
 }

+ 2 - 2
example/backend.go

@@ -182,6 +182,6 @@ func (cs *composeService) Logs(ctx context.Context, projectName string, consumer
 func (cs *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
 	return nil, errdefs.ErrNotImplemented
 }
-func (cs *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
-	return "", errdefs.ErrNotImplemented
+func (cs *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
+	return errdefs.ErrNotImplemented
 }

+ 1 - 1
local/compose/convergence.go

@@ -229,7 +229,7 @@ func (s *composeService) restartContainer(ctx context.Context, container moby.Co
 }
 
 func (s *composeService) createMobyContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int, container *moby.Container, autoRemove bool) error {
-	containerConfig, hostConfig, networkingConfig, err := getContainerCreateOptions(project, service, number, container, autoRemove)
+	containerConfig, hostConfig, networkingConfig, err := getCreateOptions(project, service, number, container, autoRemove)
 	if err != nil {
 		return err
 	}

+ 1 - 1
local/compose/create.go

@@ -91,7 +91,7 @@ func (s *composeService) ensureProjectVolumes(ctx context.Context, project *type
 	return nil
 }
 
-func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number int, inherit *moby.Container, autoRemove bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
+func getCreateOptions(p *types.Project, s types.ServiceConfig, number int, inherit *moby.Container, autoRemove bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
 	hash, err := jsonHash(s)
 	if err != nil {
 		return nil, nil, nil, err

+ 13 - 14
local/compose/run.go

@@ -19,7 +19,6 @@ package compose
 import (
 	"context"
 	"fmt"
-	"os"
 
 	"github.com/compose-spec/compose-go/types"
 	"github.com/docker/compose-cli/api/compose"
@@ -30,7 +29,7 @@ import (
 	moby "github.com/docker/docker/pkg/stringid"
 )
 
-func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (string, error) {
+func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
 	originalServices := project.Services
 	var requestedService types.ServiceConfig
 	for _, service := range originalServices {
@@ -53,16 +52,20 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
 	requestedService.Labels = requestedService.Labels.Add(oneoffLabel, "True")
 
 	if err := s.waitDependencies(ctx, project, requestedService); err != nil {
-		return "", err
+		return err
 	}
 	if err := s.createContainer(ctx, project, requestedService, requestedService.ContainerName, 1, opts.AutoRemove); err != nil {
-		return "", err
+		return err
 	}
-
 	containerID := requestedService.ContainerName
 
 	if opts.Detach {
-		return containerID, s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
+		err := s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
+		if err != nil {
+			return err
+		}
+		fmt.Fprintln(opts.Writer, containerID)
+		return nil
 	}
 
 	containers, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{
@@ -72,20 +75,16 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
 		All: true,
 	})
 	if err != nil {
-		return "", err
+		return err
 	}
 	oneoffContainer := containers[0]
 	eg := errgroup.Group{}
 	eg.Go(func() error {
-		return s.attachContainerStreams(ctx, oneoffContainer, true, os.Stdin, os.Stdout)
+		return s.attachContainerStreams(ctx, oneoffContainer, true, opts.Reader, opts.Writer)
 	})
-	if err != nil {
-		return "", err
-	}
 
 	if err = s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{}); err != nil {
-		return "", err
+		return err
 	}
-	err = eg.Wait()
-	return containerID, err
+	return eg.Wait()
 }