only stop/remove selected services

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-04-20 15:56:48 +02:00
parent d86649140f
commit 5cead2266d
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
6 changed files with 24 additions and 6 deletions

View File

@ -126,6 +126,8 @@ type RestartOptions struct {
type StopOptions struct { type StopOptions struct {
// Timeout override container stop timeout // 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 // UpOptions group options of the Up API
@ -187,6 +189,8 @@ type RemoveOptions struct {
Volumes bool Volumes bool
// Force don't ask to confirm removal // 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 // RunOptions group options of the Run API

View File

@ -32,7 +32,7 @@ import (
"github.com/docker/compose-cli/utils" "github.com/docker/compose-cli/utils"
"github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/stringid"
units "github.com/docker/go-units" "github.com/docker/go-units"
) )
type imageOptions struct { type imageOptions struct {

View File

@ -73,7 +73,9 @@ func runRemove(ctx context.Context, opts removeOptions, services []string) error
if opts.stop { if opts.stop {
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) { _, 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 return "", err
}) })
if err != nil { if err != nil {
@ -82,7 +84,8 @@ func runRemove(ctx context.Context, opts removeOptions, services []string) error
} }
reosurces, err := c.ComposeService().Remove(ctx, project, compose.RemoveOptions{ reosurces, err := c.ComposeService().Remove(ctx, project, compose.RemoveOptions{
DryRun: true, DryRun: true,
Services: services,
}) })
if err != nil { if err != nil {
return err return err

View File

@ -69,7 +69,8 @@ func runStop(ctx context.Context, opts stopOptions, services []string) error {
} }
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) { _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
return "", c.ComposeService().Stop(ctx, project, compose.StopOptions{ return "", c.ComposeService().Stop(ctx, project, compose.StopOptions{
Timeout: timeout, Timeout: timeout,
Services: services,
}) })
}) })
return err return err

View File

@ -29,7 +29,12 @@ import (
) )
func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) { 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -27,8 +27,13 @@ import (
func (s *composeService) Stop(ctx context.Context, project *types.Project, options compose.StopOptions) error { func (s *composeService) Stop(ctx context.Context, project *types.Project, options compose.StopOptions) error {
w := progress.ContextWriter(ctx) w := progress.ContextWriter(ctx)
services := options.Services
if len(services) == 0 {
services = project.ServiceNames()
}
var containers Containers 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 { if err != nil {
return err return err
} }