From c76dd110c73327ae3776828602f158d2618ead1e Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 20 May 2021 08:09:14 +0200 Subject: [PATCH] only (re)start selected services Signed-off-by: Nicolas De Loof --- api/compose/api.go | 2 ++ cli/cmd/compose/restart.go | 3 ++- local/compose/restart.go | 11 ++++++++++- local/compose/start.go | 10 +++++++++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/api/compose/api.go b/api/compose/api.go index fca9ba510..41a250f4b 100644 --- a/api/compose/api.go +++ b/api/compose/api.go @@ -122,6 +122,8 @@ type StartOptions struct { type RestartOptions struct { // Timeout override container restart timeout Timeout *time.Duration + // Services passed in the command line to be restarted + Services []string } // StopOptions group options of the Stop API diff --git a/cli/cmd/compose/restart.go b/cli/cmd/compose/restart.go index f1e031afa..225b3ef46 100644 --- a/cli/cmd/compose/restart.go +++ b/cli/cmd/compose/restart.go @@ -57,7 +57,8 @@ func runRestart(ctx context.Context, backend compose.Service, opts restartOption timeout := time.Duration(opts.timeout) * time.Second _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { return "", backend.Restart(ctx, project, compose.RestartOptions{ - Timeout: &timeout, + Timeout: &timeout, + Services: services, }) }) return err diff --git a/local/compose/restart.go b/local/compose/restart.go index 4a273ec6b..1c1038968 100644 --- a/local/compose/restart.go +++ b/local/compose/restart.go @@ -20,6 +20,7 @@ import ( "context" "github.com/docker/compose-cli/api/compose" + "github.com/docker/compose-cli/utils" "github.com/compose-spec/compose-go/types" ) @@ -29,8 +30,16 @@ func (s *composeService) Restart(ctx context.Context, project *types.Project, op if err != nil { return err } + + if len(options.Services) == 0 { + options.Services = project.ServiceNames() + } + err = InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error { - return s.restartService(ctx, service.Name, options.Timeout) + if utils.StringContains(options.Services, service.Name) { + return s.restartService(ctx, service.Name, options.Timeout) + } + return nil }) if err != nil { return err diff --git a/local/compose/start.go b/local/compose/start.go index 518873977..b0f6354d4 100644 --- a/local/compose/start.go +++ b/local/compose/start.go @@ -20,6 +20,7 @@ import ( "context" "github.com/docker/compose-cli/api/compose" + "github.com/docker/compose-cli/utils" "github.com/compose-spec/compose-go/types" moby "github.com/docker/docker/api/types" @@ -28,6 +29,10 @@ import ( ) func (s *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error { + if len(options.Services) == 0 { + options.Services = project.ServiceNames() + } + var containers Containers if options.Attach != nil { c, err := s.attach(ctx, project, options.Attach, options.Services) @@ -38,7 +43,10 @@ func (s *composeService) Start(ctx context.Context, project *types.Project, opti } err := InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error { - return s.startService(ctx, project, service) + if utils.StringContains(options.Services, service.Name) { + return s.startService(ctx, project, service) + } + return nil }) if err != nil { return err