introduce ability to select service to be stopped by compose down

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-05-10 15:21:34 +02:00 committed by Nicolas De loof
parent 68c462e607
commit 93bd27a0cc
6 changed files with 28 additions and 6 deletions

View file

@ -232,6 +232,8 @@ type DownOptions struct {
Images string
// Volumes remove volumes, both declared in the `volumes` section and anonymous ones
Volumes bool
// Services passed in the command line to be stopped
Services []string
}
// ConfigOptions group options of the Config API

View file

@ -44,7 +44,7 @@ func (s *composeService) Down(ctx context.Context, projectName string, options a
}, s.stdinfo())
}
func (s *composeService) down(ctx context.Context, projectName string, options api.DownOptions) error {
func (s *composeService) down(ctx context.Context, projectName string, options api.DownOptions) error { //golint:nocyclo
w := progress.ContextWriter(ctx)
resourceToRemove := false
@ -70,6 +70,9 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
}
err = InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
if len(options.Services) > 0 && !utils.StringContains(options.Services, service) {
return nil
}
serviceContainers := containers.filter(isService(service))
err := s.removeContainers(ctx, w, serviceContainers, options.Timeout, options.Volumes)
return err

View file

@ -102,6 +102,14 @@ func TestLocalComposeUp(t *testing.T) {
res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-words-1 gtardif/sentences-api latest`})
})
t.Run("down SERVICE", func(t *testing.T) {
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down", "web")
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "ps")
assert.Assert(t, !strings.Contains(res.Combined(), "compose-e2e-demo-web-1"), res.Combined())
assert.Assert(t, strings.Contains(res.Combined(), "compose-e2e-demo-db-1"), res.Combined())
})
t.Run("down", func(t *testing.T) {
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
})

View file

@ -153,6 +153,15 @@ func RemovedEvent(id string) Event {
return NewEvent(id, Done, "Removed")
}
// SkippedEvent creates a new Skipped Event
func SkippedEvent(id string, reason string) Event {
return Event{
ID: id,
Status: Warning,
StatusText: "Skipped: " + reason,
}
}
// NewEvent new event
func NewEvent(id string, status EventStatus, statusText string) Event {
return Event{