Skip to content

Commit 23fef85

Browse files
ndeloofglours
authored andcommitted
prefer use of slices.DeleteFunc
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 12b73be commit 23fef85

5 files changed

Lines changed: 14 additions & 150 deletions

File tree

pkg/compose/create.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"fmt"
2525
"os"
2626
"path/filepath"
27+
"slices"
2728
"strconv"
2829
"strings"
2930

@@ -32,7 +33,6 @@ import (
3233
"github.com/docker/compose/v2/pkg/api"
3334
"github.com/docker/compose/v2/pkg/progress"
3435
"github.com/docker/compose/v2/pkg/prompt"
35-
"github.com/docker/compose/v2/pkg/utils"
3636
"github.com/docker/docker/api/types/blkiodev"
3737
"github.com/docker/docker/api/types/container"
3838
"github.com/docker/docker/api/types/filters"
@@ -1312,8 +1312,8 @@ func (s *composeService) resolveOrCreateNetwork(ctx context.Context, project *ty
13121312
}
13131313

13141314
// NetworkList Matches all or part of a network name, so we have to filter for a strict match
1315-
networks = utils.Filter(networks, func(net network.Summary) bool {
1316-
return net.Name == n.Name
1315+
networks = slices.DeleteFunc(networks, func(net network.Summary) bool {
1316+
return net.Name != n.Name
13171317
})
13181318

13191319
for _, net := range networks {
@@ -1436,18 +1436,19 @@ func (s *composeService) resolveExternalNetwork(ctx context.Context, n *types.Ne
14361436
if len(networks) == 0 {
14371437
// in this instance, n.Name is really an ID
14381438
sn, err := s.apiClient().NetworkInspect(ctx, n.Name, network.InspectOptions{})
1439-
if err != nil && !errdefs.IsNotFound(err) {
1439+
if err == nil {
1440+
networks = append(networks, sn)
1441+
} else if !errdefs.IsNotFound(err) {
14401442
return "", err
14411443
}
1442-
networks = append(networks, sn)
1444+
14431445
}
14441446

14451447
// NetworkList API doesn't return the exact name match, so we can retrieve more than one network with a request
1446-
networks = utils.Filter(networks, func(net network.Inspect) bool {
1447-
// later in this function, the name is changed the to ID.
1448+
networks = slices.DeleteFunc(networks, func(net network.Inspect) bool {
14481449
// this function is called during the rebuild stage of `compose watch`.
14491450
// we still require just one network back, but we need to run the search on the ID
1450-
return net.Name == n.Name || net.ID == n.Name
1451+
return net.Name != n.Name && net.ID != n.Name
14511452
})
14521453

14531454
switch len(networks) {

pkg/compose/run.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import (
2222
"fmt"
2323
"os"
2424
"os/signal"
25+
"slices"
2526

2627
"github.com/compose-spec/compose-go/v2/types"
2728
"github.com/docker/cli/cli"
2829
cmd "github.com/docker/cli/cli/command/container"
2930
"github.com/docker/compose/v2/pkg/api"
30-
"github.com/docker/compose/v2/pkg/utils"
3131
"github.com/docker/docker/pkg/stringid"
3232
)
3333

@@ -130,11 +130,11 @@ func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts
130130

131131
if len(opts.CapAdd) > 0 {
132132
service.CapAdd = append(service.CapAdd, opts.CapAdd...)
133-
service.CapDrop = utils.Remove(service.CapDrop, opts.CapAdd...)
133+
service.CapDrop = slices.DeleteFunc(service.CapDrop, func(e string) bool { return slices.Contains(opts.CapAdd, e) })
134134
}
135135
if len(opts.CapDrop) > 0 {
136136
service.CapDrop = append(service.CapDrop, opts.CapDrop...)
137-
service.CapAdd = utils.Remove(service.CapAdd, opts.CapDrop...)
137+
service.CapAdd = slices.DeleteFunc(service.CapAdd, func(e string) bool { return slices.Contains(opts.CapDrop, e) })
138138
}
139139
if opts.WorkingDir != "" {
140140
service.WorkingDir = opts.WorkingDir

pkg/compose/start.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
265265
if _, ok := watched[container.ID]; ok {
266266
eType := api.ContainerEventStopped
267267
if slices.Contains(replaced, container.ID) {
268-
utils.Remove(replaced, container.ID)
268+
replaced = slices.DeleteFunc(replaced, func(e string) bool { return e == container.ID })
269269
eType = api.ContainerEventRecreated
270270
}
271271
listener(api.ContainerEvent{
@@ -292,7 +292,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo
292292

293293
eType := api.ContainerEventExit
294294
if slices.Contains(replaced, container.ID) {
295-
utils.Remove(replaced, container.ID)
295+
replaced = slices.DeleteFunc(replaced, func(e string) bool { return e == container.ID })
296296
eType = api.ContainerEventRecreated
297297
}
298298

pkg/utils/slices.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

pkg/utils/slices_test.go

Lines changed: 0 additions & 95 deletions
This file was deleted.

0 commit comments

Comments
 (0)