diff --git a/local/compose/containers.go b/local/compose/containers.go index 470c96ff2..0e995b812 100644 --- a/local/compose/containers.go +++ b/local/compose/containers.go @@ -70,20 +70,6 @@ func (containers Containers) filter(predicate containerPredicate) Containers { return filtered } -// split return Containers with elements to match and those not to match predicate -func (containers Containers) split(predicate containerPredicate) (Containers, Containers) { - var right Containers - var left Containers - for _, c := range containers { - if predicate(c) { - right = append(right, c) - } else { - left = append(left, c) - } - } - return right, left -} - func (containers Containers) names() []string { var names []string for _, c := range containers { diff --git a/local/compose/down.go b/local/compose/down.go index e60912e6b..6e7cc1e92 100644 --- a/local/compose/down.go +++ b/local/compose/down.go @@ -57,17 +57,17 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c } err = InReverseDependencyOrder(ctx, options.Project, func(c context.Context, service types.ServiceConfig) error { - serviceContainers, others := containers.split(isService(service.Name)) + serviceContainers := containers.filter(isService(service.Name)) err := s.removeContainers(ctx, w, serviceContainers) - containers = others return err }) if err != nil { return err } - if options.RemoveOrphans && len(containers) > 0 { - err := s.removeContainers(ctx, w, containers) + orphans := containers.filter(isNotService(options.Project.ServiceNames()...)) + if options.RemoveOrphans && len(orphans) > 0 { + err := s.removeContainers(ctx, w, orphans) if err != nil { return err } diff --git a/local/compose/down_test.go b/local/compose/down_test.go index 0f876e5f1..7ebf0af9e 100644 --- a/local/compose/down_test.go +++ b/local/compose/down_test.go @@ -64,9 +64,7 @@ func TestDownRemoveOrphans(t *testing.T) { ctx := context.Background() api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return( - []apitypes.Container{testContainer("service1", "123"), - testContainer("service2", "789"), - testContainer("service_orphan", "321")}, nil).Times(2) + []apitypes.Container{testContainer("service1", "123"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil).Times(2) api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil) api.EXPECT().ContainerStop(ctx, "789", nil).Return(nil)