From 0914f1296c71b3810d74eff8bb91b628a668b4f4 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 20 Apr 2021 15:56:48 +0200 Subject: [PATCH] only stop/remove selected services Signed-off-by: Nicolas De Loof --- api/compose/api.go | 8 ++++++-- cli/cmd/compose/remove.go | 5 ++++- cli/cmd/compose/stop.go | 1 + local/compose/remove.go | 7 ++++++- local/compose/stop.go | 7 ++++++- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/api/compose/api.go b/api/compose/api.go index 3014401fed..a2832e8fb4 100644 --- a/api/compose/api.go +++ b/api/compose/api.go @@ -125,7 +125,9 @@ type RestartOptions struct { // StopOptions group options of the Stop API type StopOptions struct { // Timeout override container stop timeout - Timeout *time.Duration + Timeout *time.Duration + // Services passed in the command line to be stopped + Services []string } // UpOptions group options of the Up API @@ -186,7 +188,9 @@ type RemoveOptions struct { // Volumes remove anonymous volumes Volumes bool // Force don't ask to confirm removal - Force bool + Force bool + // Services passed in the command line to be removed + Services []string } // RunOptions group options of the Run API diff --git a/cli/cmd/compose/remove.go b/cli/cmd/compose/remove.go index 753f8300aa..0f7760880d 100644 --- a/cli/cmd/compose/remove.go +++ b/cli/cmd/compose/remove.go @@ -73,7 +73,9 @@ func runRemove(ctx context.Context, opts removeOptions, services []string) error if opts.stop { _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - err := c.ComposeService().Stop(ctx, project, compose.StopOptions{}) + err := c.ComposeService().Stop(ctx, project, compose.StopOptions{ + Services: services, + }) return "", err }) if err != nil { @@ -83,6 +85,7 @@ func runRemove(ctx context.Context, opts removeOptions, services []string) error reosurces, err := c.ComposeService().Remove(ctx, project, compose.RemoveOptions{ DryRun: true, + Services: services, }) if err != nil { return err diff --git a/cli/cmd/compose/stop.go b/cli/cmd/compose/stop.go index f80dc1c949..a867fb458c 100644 --- a/cli/cmd/compose/stop.go +++ b/cli/cmd/compose/stop.go @@ -70,6 +70,7 @@ func runStop(ctx context.Context, opts stopOptions, services []string) error { _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { return "", c.ComposeService().Stop(ctx, project, compose.StopOptions{ Timeout: timeout, + Services: services, }) }) return err diff --git a/local/compose/remove.go b/local/compose/remove.go index 1018bc34df..713077d2c2 100644 --- a/local/compose/remove.go +++ b/local/compose/remove.go @@ -29,7 +29,12 @@ import ( ) func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) { - containers, err := s.getContainers(ctx, project.Name, oneOffInclude, true) + services := options.Services + if len(services) == 0 { + services = project.ServiceNames() + } + + containers, err := s.getContainers(ctx, project.Name, oneOffInclude, true, services...) if err != nil { return nil, err } diff --git a/local/compose/stop.go b/local/compose/stop.go index b528312dbd..0e950e440c 100644 --- a/local/compose/stop.go +++ b/local/compose/stop.go @@ -27,8 +27,13 @@ import ( func (s *composeService) Stop(ctx context.Context, project *types.Project, options compose.StopOptions) error { w := progress.ContextWriter(ctx) + + services := options.Services + if len(services) == 0 { + services = project.ServiceNames() + } var containers Containers - containers, err := s.getContainers(ctx, project.Name, oneOffInclude, true, project.ServiceNames()...) + containers, err := s.getContainers(ctx, project.Name, oneOffInclude, true, services...) if err != nil { return err }