diff --git a/pkg/compose/dependencies.go b/pkg/compose/dependencies.go index 19660e8e8..812e45c98 100644 --- a/pkg/compose/dependencies.go +++ b/pkg/compose/dependencies.go @@ -26,8 +26,6 @@ import ( "github.com/compose-spec/compose-go/v2/types" "github.com/docker/compose/v2/pkg/api" "golang.org/x/sync/errgroup" - - "github.com/docker/compose/v2/pkg/utils" ) // ServiceStatus indicates the status of a service @@ -120,7 +118,7 @@ func WithRootNodesAndDown(nodes []string) func(*graphTraversal) { t.ignored = map[string]struct{}{} for k := range graph.Vertices { - if !utils.Contains(want, k) { + if !slices.Contains(want, k) { t.ignored[k] = struct{}{} } } diff --git a/pkg/compose/generate.go b/pkg/compose/generate.go index 157c1384e..61906b949 100644 --- a/pkg/compose/generate.go +++ b/pkg/compose/generate.go @@ -19,11 +19,11 @@ package compose import ( "context" "fmt" + "slices" "strings" "github.com/compose-spec/compose-go/v2/types" "github.com/docker/compose/v2/pkg/api" - "github.com/docker/compose/v2/pkg/utils" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/mount" @@ -54,8 +54,11 @@ func (s *composeService) Generate(ctx context.Context, options api.GenerateOptio if err != nil { return nil, err } + for _, ctr := range containersByIds { - if !utils.Contains(containers, ctr) { + if !slices.ContainsFunc(containers, func(summary container.Summary) bool { + return summary.ID == ctr.ID + }) { containers = append(containers, ctr) } } diff --git a/pkg/compose/start.go b/pkg/compose/start.go index 06d73d9cc..fafeed15f 100644 --- a/pkg/compose/start.go +++ b/pkg/compose/start.go @@ -20,6 +20,7 @@ import ( "context" "errors" "fmt" + "slices" "strings" "time" @@ -199,7 +200,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo ofInterest := func(c containerType.Summary) bool { if len(services) > 0 { // we only watch some services - return utils.Contains(services, c.Labels[api.ServiceLabel]) + return slices.Contains(services, c.Labels[api.ServiceLabel]) } return true } @@ -208,7 +209,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo isRequired := func(c containerType.Summary) bool { if len(services) > 0 && len(required) > 0 { // we only watch some services - return utils.Contains(required, c.Labels[api.ServiceLabel]) + return slices.Contains(required, c.Labels[api.ServiceLabel]) } return true } @@ -263,7 +264,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo } if _, ok := watched[container.ID]; ok { eType := api.ContainerEventStopped - if utils.Contains(replaced, container.ID) { + if slices.Contains(replaced, container.ID) { utils.Remove(replaced, container.ID) eType = api.ContainerEventRecreated } @@ -290,7 +291,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo } eType := api.ContainerEventExit - if utils.Contains(replaced, container.ID) { + if slices.Contains(replaced, container.ID) { utils.Remove(replaced, container.ID) eType = api.ContainerEventRecreated } diff --git a/pkg/utils/slices.go b/pkg/utils/slices.go index 6dbf1c6e7..951c431bc 100644 --- a/pkg/utils/slices.go +++ b/pkg/utils/slices.go @@ -16,24 +16,15 @@ package utils -import "reflect" - -// Contains helps to detect if a non-comparable struct is part of an array -// only use this method if you can't rely on existing golang Contains function of slices (https://pkg.go.dev/golang.org/x/exp/slices#Contains) -func Contains[T any](origin []T, element T) bool { - for _, v := range origin { - if reflect.DeepEqual(v, element) { - return true - } - } - return false -} +import ( + "slices" +) // Remove removes all elements from origin slice -func Remove[T any](origin []T, elements ...T) []T { +func Remove[T comparable](origin []T, elements ...T) []T { var filtered []T for _, v := range origin { - if !Contains(elements, v) { + if !slices.Contains(elements, v) { filtered = append(filtered, v) } }