From 10d024428c2d2277388f269982d5e3d2afc12e3a Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Tue, 2 Mar 2021 22:27:34 -0300 Subject: [PATCH] Add --attach-dependencies This actually makes the service selection effective and add the flag to disable the service selection. Signed-off-by: Ulysses Souza --- api/compose/api.go | 2 ++ cli/cmd/compose/up.go | 43 +++++++++++++++++++++---------------- local/compose/attach.go | 4 ++-- local/compose/containers.go | 7 ++++-- local/compose/pause.go | 4 ++-- local/compose/remove.go | 2 +- local/compose/start.go | 2 +- 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/api/compose/api.go b/api/compose/api.go index a7156497b..98067a507 100644 --- a/api/compose/api.go +++ b/api/compose/api.go @@ -93,6 +93,8 @@ type CreateOptions struct { type StartOptions struct { // Attach will attach to service containers and send container logs and events Attach ContainerEventListener + // Services passed in the command line to be started + Services []string } // StopOptions group options of the Stop API diff --git a/cli/cmd/compose/up.go b/cli/cmd/compose/up.go index a97e0d09c..aa7318902 100644 --- a/cli/cmd/compose/up.go +++ b/cli/cmd/compose/up.go @@ -51,22 +51,23 @@ type composeOptions struct { type upOptions struct { *composeOptions - Detach bool - Environment []string - removeOrphans bool - forceRecreate bool - noRecreate bool - recreateDeps bool - noStart bool - noDeps bool - cascadeStop bool - exitCodeFrom string - scale []string - noColor bool - noPrefix bool - timeChanged bool - timeout int - noInherit bool + Detach bool + Environment []string + removeOrphans bool + forceRecreate bool + noRecreate bool + recreateDeps bool + noStart bool + noDeps bool + cascadeStop bool + exitCodeFrom string + scale []string + noColor bool + noPrefix bool + timeChanged bool + timeout int + noInherit bool + attachDependencies bool } func (opts upOptions) recreateStrategy() string { @@ -156,8 +157,8 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command { if opts.Build && opts.noBuild { return fmt.Errorf("--build and --no-build are incompatible") } - if opts.cascadeStop && opts.Detach { - return fmt.Errorf("--abort-on-container-exit and --detach are incompatible") + if opts.Detach && (opts.attachDependencies || opts.cascadeStop) { + return fmt.Errorf("--detach cannot be combined with --abort-on-container-exit or --attach-dependencies") } if opts.forceRecreate && opts.noRecreate { return fmt.Errorf("--force-recreate and --no-recreate are incompatible") @@ -194,6 +195,7 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command { flags.BoolVar(&opts.noDeps, "no-deps", false, "Don't start linked services.") flags.BoolVar(&opts.recreateDeps, "always-recreate-deps", false, "Recreate dependent containers. Incompatible with --no-recreate.") flags.BoolVarP(&opts.noInherit, "renew-anon-volumes", "V", false, "Recreate anonymous volumes instead of retrieving data from the previous containers.") + flags.BoolVar(&opts.attachDependencies, "attach-dependencies", false, "Attach to dependent containers.") } return upCmd @@ -254,6 +256,10 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro return nil } + if opts.attachDependencies { + services = nil + } + if opts.Detach { return nil } @@ -295,6 +301,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro Attach: func(event compose.ContainerEvent) { queue <- event }, + Services: services, }) if err != nil { return err diff --git a/local/compose/attach.go b/local/compose/attach.go index 7394a11b1..7ebfa51c0 100644 --- a/local/compose/attach.go +++ b/local/compose/attach.go @@ -31,8 +31,8 @@ import ( "github.com/docker/docker/pkg/stdcopy" ) -func (s *composeService) attach(ctx context.Context, project *types.Project, consumer compose.ContainerEventListener) (Containers, error) { - containers, err := s.getContainers(ctx, project, oneOffExclude) +func (s *composeService) attach(ctx context.Context, project *types.Project, consumer compose.ContainerEventListener, selectedServices []string) (Containers, error) { + containers, err := s.getContainers(ctx, project, oneOffExclude, selectedServices) if err != nil { return nil, err } diff --git a/local/compose/containers.go b/local/compose/containers.go index 7628aac92..4d6de3b1a 100644 --- a/local/compose/containers.go +++ b/local/compose/containers.go @@ -37,7 +37,7 @@ const ( oneOffOnly ) -func (s *composeService) getContainers(ctx context.Context, project *types.Project, oneOff oneOff) (Containers, error) { +func (s *composeService) getContainers(ctx context.Context, project *types.Project, oneOff oneOff, selectedServices []string) (Containers, error) { var containers Containers f := filters.NewArgs( projectFilter(project.Name), @@ -56,7 +56,10 @@ func (s *composeService) getContainers(ctx context.Context, project *types.Proje if err != nil { return nil, err } - containers = containers.filter(isService(project.ServiceNames()...)) + if len(selectedServices) == 0 { + selectedServices = project.ServiceNames() + } + containers = containers.filter(isService(selectedServices...)) return containers, nil } diff --git a/local/compose/pause.go b/local/compose/pause.go index 7af67dd1a..b4a0d0bbd 100644 --- a/local/compose/pause.go +++ b/local/compose/pause.go @@ -27,7 +27,7 @@ import ( ) func (s *composeService) Pause(ctx context.Context, project *types.Project) error { - containers, err := s.getContainers(ctx, project, oneOffExclude) + containers, err := s.getContainers(ctx, project, oneOffExclude, nil) if err != nil { return err } @@ -49,7 +49,7 @@ func (s *composeService) Pause(ctx context.Context, project *types.Project) erro } func (s *composeService) UnPause(ctx context.Context, project *types.Project) error { - containers, err := s.getContainers(ctx, project, oneOffExclude) + containers, err := s.getContainers(ctx, project, oneOffExclude, nil) if err != nil { return err } diff --git a/local/compose/remove.go b/local/compose/remove.go index 76444eeb6..c69bf0309 100644 --- a/local/compose/remove.go +++ b/local/compose/remove.go @@ -29,7 +29,7 @@ import ( ) func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) { - containers, err := s.getContainers(ctx, project, oneOffInclude) + containers, err := s.getContainers(ctx, project, oneOffInclude, nil) if err != nil { return nil, err } diff --git a/local/compose/start.go b/local/compose/start.go index 8222e4344..105825474 100644 --- a/local/compose/start.go +++ b/local/compose/start.go @@ -29,7 +29,7 @@ import ( func (s *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error { var containers Containers if options.Attach != nil { - c, err := s.attach(ctx, project, options.Attach) + c, err := s.attach(ctx, project, options.Attach, options.Services) if err != nil { return err }