Skip to content

Commit 2e71440

Browse files
tongjicoderndeloof
authored andcommitted
refactor: use slices.Contains to simplify code
Signed-off-by: tongjicoder <tongjicoder@icloud.com>
1 parent d49a68e commit 2e71440

14 files changed

Lines changed: 32 additions & 47 deletions

File tree

cmd/compose/images.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23+
"slices"
2324
"sort"
2425
"strings"
2526

@@ -30,7 +31,6 @@ import (
3031

3132
"github.com/docker/compose/v2/cmd/formatter"
3233
"github.com/docker/compose/v2/pkg/api"
33-
"github.com/docker/compose/v2/pkg/utils"
3434
)
3535

3636
type imageOptions struct {
@@ -76,7 +76,7 @@ func runImages(ctx context.Context, dockerCli command.Cli, backend api.Service,
7676
if i := strings.IndexRune(img.ID, ':'); i >= 0 {
7777
id = id[i+1:]
7878
}
79-
if !utils.StringContains(ids, id) {
79+
if !slices.Contains(ids, id) {
8080
ids = append(ids, id)
8181
}
8282
}

cmd/compose/options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io"
2323
"os"
24+
"slices"
2425
"sort"
2526
"strings"
2627
"text/tabwriter"
@@ -32,7 +33,6 @@ import (
3233
"github.com/docker/compose/v2/internal/tracing"
3334
ui "github.com/docker/compose/v2/pkg/progress"
3435
"github.com/docker/compose/v2/pkg/prompt"
35-
"github.com/docker/compose/v2/pkg/utils"
3636
)
3737

3838
func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
@@ -44,15 +44,15 @@ func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
4444

4545
// default platform only applies if the service doesn't specify
4646
if defaultPlatform != "" && service.Platform == "" {
47-
if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, defaultPlatform) {
47+
if len(service.Build.Platforms) > 0 && !slices.Contains(service.Build.Platforms, defaultPlatform) {
4848
return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", name, defaultPlatform)
4949
}
5050
service.Platform = defaultPlatform
5151
}
5252

5353
if service.Platform != "" {
5454
if len(service.Build.Platforms) > 0 {
55-
if !utils.StringContains(service.Build.Platforms, service.Platform) {
55+
if !slices.Contains(service.Build.Platforms, service.Platform) {
5656
return fmt.Errorf("service %q build configuration does not support platform: %s", name, service.Platform)
5757
}
5858
}

cmd/compose/ps.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"slices"
2324
"sort"
2425
"strings"
2526

2627
"github.com/docker/compose/v2/cmd/formatter"
2728
"github.com/docker/compose/v2/pkg/api"
28-
"github.com/docker/compose/v2/pkg/utils"
2929

3030
"github.com/docker/cli/cli/command"
3131
cliformatter "github.com/docker/cli/cli/command/formatter"
@@ -101,7 +101,7 @@ func runPs(ctx context.Context, dockerCli command.Cli, backend api.Service, serv
101101
names := project.ServiceNames()
102102
if len(services) > 0 {
103103
for _, service := range services {
104-
if !utils.StringContains(names, service) {
104+
if !slices.Contains(names, service) {
105105
return fmt.Errorf("no such service: %s", service)
106106
}
107107
}
@@ -139,7 +139,7 @@ func runPs(ctx context.Context, dockerCli command.Cli, backend api.Service, serv
139139
services := []string{}
140140
for _, c := range containers {
141141
s := c.Service
142-
if !utils.StringContains(services, s) {
142+
if !slices.Contains(services, s) {
143143
services = append(services, s)
144144
}
145145
}

internal/ocipush/push.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"net/http"
2525
"path/filepath"
26+
"slices"
2627
"time"
2728

2829
pusherrors "github.com/containerd/containerd/v2/core/remotes/errors"
@@ -157,14 +158,7 @@ func isNonAuthClientError(statusCode int) bool {
157158
// not a client error
158159
return false
159160
}
160-
for _, v := range clientAuthStatusCodes {
161-
if statusCode == v {
162-
// client auth error
163-
return false
164-
}
165-
}
166-
// any other 4xx client error
167-
return true
161+
return !slices.Contains(clientAuthStatusCodes, statusCode)
168162
}
169163

170164
func generateManifest(layers []v1.Descriptor, ociCompat api.OCIVersion) ([]Pushable, error) {

pkg/api/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ package api
1919
import (
2020
"context"
2121
"fmt"
22+
"slices"
2223
"strings"
2324
"time"
2425

2526
"github.com/compose-spec/compose-go/v2/types"
2627
"github.com/docker/cli/opts"
27-
"github.com/docker/compose/v2/pkg/utils"
2828
)
2929

3030
// Service manages a compose project
@@ -175,13 +175,13 @@ func (o BuildOptions) Apply(project *types.Project) error {
175175
continue
176176
}
177177
if platform != "" {
178-
if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, platform) {
178+
if len(service.Build.Platforms) > 0 && !slices.Contains(service.Build.Platforms, platform) {
179179
return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", name, platform)
180180
}
181181
service.Platform = platform
182182
}
183183
if service.Platform != "" {
184-
if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, service.Platform) {
184+
if len(service.Build.Platforms) > 0 && !slices.Contains(service.Build.Platforms, service.Platform) {
185185
return fmt.Errorf("service %q build configuration does not support platform: %s", name, service.Platform)
186186
}
187187
}

pkg/compose/containers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ package compose
1919
import (
2020
"context"
2121
"fmt"
22+
"slices"
2223
"sort"
2324
"strconv"
2425

2526
"github.com/compose-spec/compose-go/v2/types"
2627
"github.com/docker/compose/v2/pkg/api"
27-
"github.com/docker/compose/v2/pkg/utils"
2828
"github.com/docker/docker/api/types/container"
2929
"github.com/docker/docker/api/types/filters"
3030
)
@@ -124,7 +124,7 @@ func matches(c container.Summary, predicates ...containerPredicate) bool {
124124
func isService(services ...string) containerPredicate {
125125
return func(c container.Summary) bool {
126126
service := c.Labels[api.ServiceLabel]
127-
return utils.StringContains(services, service)
127+
return slices.Contains(services, service)
128128
}
129129
}
130130

@@ -145,7 +145,7 @@ func isOrphaned(project *types.Project) containerPredicate {
145145
}
146146
// Service that is not defined in the compose model
147147
service := c.Labels[api.ServiceLabel]
148-
return !utils.StringContains(services, service)
148+
return !slices.Contains(services, service)
149149
}
150150
}
151151

pkg/compose/convergence.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (c *convergence) apply(ctx context.Context, project *types.Project, options
101101

102102
return tracing.SpanWrapFunc("service/apply", tracing.ServiceOptions(service), func(ctx context.Context) error {
103103
strategy := options.RecreateDependencies
104-
if utils.StringContains(options.Services, name) {
104+
if slices.Contains(options.Services, name) {
105105
strategy = options.Recreate
106106
}
107107
return c.ensureService(ctx, project, service, strategy, options.Inherit, options.Timeout)

pkg/compose/dependencies.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package compose
1919
import (
2020
"context"
2121
"fmt"
22+
"slices"
2223
"strings"
2324
"sync"
2425

@@ -434,7 +435,7 @@ func (g *Graph) HasCycles() (bool, error) {
434435
path := []string{
435436
vertex.Key,
436437
}
437-
if !utils.StringContains(discovered, vertex.Key) && !utils.StringContains(finished, vertex.Key) {
438+
if !slices.Contains(discovered, vertex.Key) && !slices.Contains(finished, vertex.Key) {
438439
var err error
439440
discovered, finished, err = g.visit(vertex.Key, path, discovered, finished)
440441
if err != nil {
@@ -451,11 +452,11 @@ func (g *Graph) visit(key string, path []string, discovered []string, finished [
451452

452453
for _, v := range g.Vertices[key].Children {
453454
path := append(path, v.Key)
454-
if utils.StringContains(discovered, v.Key) {
455+
if slices.Contains(discovered, v.Key) {
455456
return nil, nil, fmt.Errorf("cycle found: %s", strings.Join(path, " -> "))
456457
}
457458

458-
if !utils.StringContains(finished, v.Key) {
459+
if !slices.Contains(finished, v.Key) {
459460
if _, _, err := g.visit(v.Key, path, discovered, finished); err != nil {
460461
return nil, nil, err
461462
}

pkg/compose/events.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ package compose
1818

1919
import (
2020
"context"
21+
"slices"
2122
"strings"
2223
"time"
2324

2425
"github.com/docker/docker/api/types/events"
2526
"github.com/docker/docker/api/types/filters"
2627

2728
"github.com/docker/compose/v2/pkg/api"
28-
"github.com/docker/compose/v2/pkg/utils"
2929
)
3030

3131
func (s *composeService) Events(ctx context.Context, projectName string, options api.EventsOptions) error {
@@ -47,7 +47,7 @@ func (s *composeService) Events(ctx context.Context, projectName string, options
4747
continue
4848
}
4949
service := event.Actor.Attributes[api.ServiceLabel]
50-
if len(options.Services) > 0 && !utils.StringContains(options.Services, service) {
50+
if len(options.Services) > 0 && !slices.Contains(options.Services, service) {
5151
continue
5252
}
5353

pkg/compose/images.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package compose
1919
import (
2020
"context"
2121
"fmt"
22+
"slices"
2223
"strings"
2324
"sync"
2425

@@ -29,7 +30,6 @@ import (
2930
"golang.org/x/sync/errgroup"
3031

3132
"github.com/docker/compose/v2/pkg/api"
32-
"github.com/docker/compose/v2/pkg/utils"
3333
)
3434

3535
func (s *composeService) Images(ctx context.Context, projectName string, options api.ImagesOptions) ([]api.ImageSummary, error) {
@@ -45,7 +45,7 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
4545
if len(options.Services) > 0 {
4646
// filter service containers
4747
for _, c := range allContainers {
48-
if utils.StringContains(options.Services, c.Labels[api.ServiceLabel]) {
48+
if slices.Contains(options.Services, c.Labels[api.ServiceLabel]) {
4949
containers = append(containers, c)
5050
}
5151
}
@@ -55,7 +55,7 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
5555

5656
images := []string{}
5757
for _, c := range containers {
58-
if !utils.StringContains(images, c.Image) {
58+
if !slices.Contains(images, c.Image) {
5959
images = append(images, c.Image)
6060
}
6161
}

0 commit comments

Comments
 (0)