Browse Source

refactor: use slices.Contains to simplify code

Signed-off-by: tongjicoder <[email protected]>
tongjicoder 6 months ago
parent
commit
2e71440bee

+ 2 - 2
cmd/compose/images.go

@@ -20,6 +20,7 @@ import (
 	"context"
 	"fmt"
 	"io"
+	"slices"
 	"sort"
 	"strings"
 
@@ -30,7 +31,6 @@ import (
 
 	"github.com/docker/compose/v2/cmd/formatter"
 	"github.com/docker/compose/v2/pkg/api"
-	"github.com/docker/compose/v2/pkg/utils"
 )
 
 type imageOptions struct {
@@ -76,7 +76,7 @@ func runImages(ctx context.Context, dockerCli command.Cli, backend api.Service,
 			if i := strings.IndexRune(img.ID, ':'); i >= 0 {
 				id = id[i+1:]
 			}
-			if !utils.StringContains(ids, id) {
+			if !slices.Contains(ids, id) {
 				ids = append(ids, id)
 			}
 		}

+ 3 - 3
cmd/compose/options.go

@@ -21,6 +21,7 @@ import (
 	"fmt"
 	"io"
 	"os"
+	"slices"
 	"sort"
 	"strings"
 	"text/tabwriter"
@@ -32,7 +33,6 @@ import (
 	"github.com/docker/compose/v2/internal/tracing"
 	ui "github.com/docker/compose/v2/pkg/progress"
 	"github.com/docker/compose/v2/pkg/prompt"
-	"github.com/docker/compose/v2/pkg/utils"
 )
 
 func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
@@ -44,7 +44,7 @@ func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
 
 		// default platform only applies if the service doesn't specify
 		if defaultPlatform != "" && service.Platform == "" {
-			if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, defaultPlatform) {
+			if len(service.Build.Platforms) > 0 && !slices.Contains(service.Build.Platforms, defaultPlatform) {
 				return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", name, defaultPlatform)
 			}
 			service.Platform = defaultPlatform
@@ -52,7 +52,7 @@ func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
 
 		if service.Platform != "" {
 			if len(service.Build.Platforms) > 0 {
-				if !utils.StringContains(service.Build.Platforms, service.Platform) {
+				if !slices.Contains(service.Build.Platforms, service.Platform) {
 					return fmt.Errorf("service %q build configuration does not support platform: %s", name, service.Platform)
 				}
 			}

+ 3 - 3
cmd/compose/ps.go

@@ -20,12 +20,12 @@ import (
 	"context"
 	"errors"
 	"fmt"
+	"slices"
 	"sort"
 	"strings"
 
 	"github.com/docker/compose/v2/cmd/formatter"
 	"github.com/docker/compose/v2/pkg/api"
-	"github.com/docker/compose/v2/pkg/utils"
 
 	"github.com/docker/cli/cli/command"
 	cliformatter "github.com/docker/cli/cli/command/formatter"
@@ -101,7 +101,7 @@ func runPs(ctx context.Context, dockerCli command.Cli, backend api.Service, serv
 		names := project.ServiceNames()
 		if len(services) > 0 {
 			for _, service := range services {
-				if !utils.StringContains(names, service) {
+				if !slices.Contains(names, service) {
 					return fmt.Errorf("no such service: %s", service)
 				}
 			}
@@ -139,7 +139,7 @@ func runPs(ctx context.Context, dockerCli command.Cli, backend api.Service, serv
 		services := []string{}
 		for _, c := range containers {
 			s := c.Service
-			if !utils.StringContains(services, s) {
+			if !slices.Contains(services, s) {
 				services = append(services, s)
 			}
 		}

+ 2 - 8
internal/ocipush/push.go

@@ -23,6 +23,7 @@ import (
 	"fmt"
 	"net/http"
 	"path/filepath"
+	"slices"
 	"time"
 
 	pusherrors "github.com/containerd/containerd/v2/core/remotes/errors"
@@ -157,14 +158,7 @@ func isNonAuthClientError(statusCode int) bool {
 		// not a client error
 		return false
 	}
-	for _, v := range clientAuthStatusCodes {
-		if statusCode == v {
-			// client auth error
-			return false
-		}
-	}
-	// any other 4xx client error
-	return true
+	return !slices.Contains(clientAuthStatusCodes, statusCode)
 }
 
 func generateManifest(layers []v1.Descriptor, ociCompat api.OCIVersion) ([]Pushable, error) {

+ 3 - 3
pkg/api/api.go

@@ -19,12 +19,12 @@ package api
 import (
 	"context"
 	"fmt"
+	"slices"
 	"strings"
 	"time"
 
 	"github.com/compose-spec/compose-go/v2/types"
 	"github.com/docker/cli/opts"
-	"github.com/docker/compose/v2/pkg/utils"
 )
 
 // Service manages a compose project
@@ -175,13 +175,13 @@ func (o BuildOptions) Apply(project *types.Project) error {
 			continue
 		}
 		if platform != "" {
-			if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, platform) {
+			if len(service.Build.Platforms) > 0 && !slices.Contains(service.Build.Platforms, platform) {
 				return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", name, platform)
 			}
 			service.Platform = platform
 		}
 		if service.Platform != "" {
-			if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, service.Platform) {
+			if len(service.Build.Platforms) > 0 && !slices.Contains(service.Build.Platforms, service.Platform) {
 				return fmt.Errorf("service %q build configuration does not support platform: %s", name, service.Platform)
 			}
 		}

+ 3 - 3
pkg/compose/containers.go

@@ -19,12 +19,12 @@ package compose
 import (
 	"context"
 	"fmt"
+	"slices"
 	"sort"
 	"strconv"
 
 	"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"
 )
@@ -124,7 +124,7 @@ func matches(c container.Summary, predicates ...containerPredicate) bool {
 func isService(services ...string) containerPredicate {
 	return func(c container.Summary) bool {
 		service := c.Labels[api.ServiceLabel]
-		return utils.StringContains(services, service)
+		return slices.Contains(services, service)
 	}
 }
 
@@ -145,7 +145,7 @@ func isOrphaned(project *types.Project) containerPredicate {
 		}
 		// Service that is not defined in the compose model
 		service := c.Labels[api.ServiceLabel]
-		return !utils.StringContains(services, service)
+		return !slices.Contains(services, service)
 	}
 }
 

+ 1 - 1
pkg/compose/convergence.go

@@ -101,7 +101,7 @@ func (c *convergence) apply(ctx context.Context, project *types.Project, options
 
 		return tracing.SpanWrapFunc("service/apply", tracing.ServiceOptions(service), func(ctx context.Context) error {
 			strategy := options.RecreateDependencies
-			if utils.StringContains(options.Services, name) {
+			if slices.Contains(options.Services, name) {
 				strategy = options.Recreate
 			}
 			return c.ensureService(ctx, project, service, strategy, options.Inherit, options.Timeout)

+ 4 - 3
pkg/compose/dependencies.go

@@ -19,6 +19,7 @@ package compose
 import (
 	"context"
 	"fmt"
+	"slices"
 	"strings"
 	"sync"
 
@@ -434,7 +435,7 @@ func (g *Graph) HasCycles() (bool, error) {
 		path := []string{
 			vertex.Key,
 		}
-		if !utils.StringContains(discovered, vertex.Key) && !utils.StringContains(finished, vertex.Key) {
+		if !slices.Contains(discovered, vertex.Key) && !slices.Contains(finished, vertex.Key) {
 			var err error
 			discovered, finished, err = g.visit(vertex.Key, path, discovered, finished)
 			if err != nil {
@@ -451,11 +452,11 @@ func (g *Graph) visit(key string, path []string, discovered []string, finished [
 
 	for _, v := range g.Vertices[key].Children {
 		path := append(path, v.Key)
-		if utils.StringContains(discovered, v.Key) {
+		if slices.Contains(discovered, v.Key) {
 			return nil, nil, fmt.Errorf("cycle found: %s", strings.Join(path, " -> "))
 		}
 
-		if !utils.StringContains(finished, v.Key) {
+		if !slices.Contains(finished, v.Key) {
 			if _, _, err := g.visit(v.Key, path, discovered, finished); err != nil {
 				return nil, nil, err
 			}

+ 2 - 2
pkg/compose/events.go

@@ -18,6 +18,7 @@ package compose
 
 import (
 	"context"
+	"slices"
 	"strings"
 	"time"
 
@@ -25,7 +26,6 @@ import (
 	"github.com/docker/docker/api/types/filters"
 
 	"github.com/docker/compose/v2/pkg/api"
-	"github.com/docker/compose/v2/pkg/utils"
 )
 
 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
 				continue
 			}
 			service := event.Actor.Attributes[api.ServiceLabel]
-			if len(options.Services) > 0 && !utils.StringContains(options.Services, service) {
+			if len(options.Services) > 0 && !slices.Contains(options.Services, service) {
 				continue
 			}
 

+ 3 - 3
pkg/compose/images.go

@@ -19,6 +19,7 @@ package compose
 import (
 	"context"
 	"fmt"
+	"slices"
 	"strings"
 	"sync"
 
@@ -29,7 +30,6 @@ import (
 	"golang.org/x/sync/errgroup"
 
 	"github.com/docker/compose/v2/pkg/api"
-	"github.com/docker/compose/v2/pkg/utils"
 )
 
 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
 	if len(options.Services) > 0 {
 		// filter service containers
 		for _, c := range allContainers {
-			if utils.StringContains(options.Services, c.Labels[api.ServiceLabel]) {
+			if slices.Contains(options.Services, c.Labels[api.ServiceLabel]) {
 				containers = append(containers, c)
 			}
 		}
@@ -55,7 +55,7 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
 
 	images := []string{}
 	for _, c := range containers {
-		if !utils.StringContains(images, c.Image) {
+		if !slices.Contains(images, c.Image) {
 			images = append(images, c.Image)
 		}
 	}

+ 2 - 2
pkg/compose/ls.go

@@ -19,11 +19,11 @@ package compose
 import (
 	"context"
 	"fmt"
+	"slices"
 	"sort"
 	"strings"
 
 	"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/sirupsen/logrus"
@@ -74,7 +74,7 @@ func combinedConfigFiles(containers []container.Summary) (string, error) {
 		}
 
 		for _, f := range strings.Split(files, ",") {
-			if !utils.StringContains(configFiles, f) {
+			if !slices.Contains(configFiles, f) {
 				configFiles = append(configFiles, f)
 			}
 		}

+ 2 - 2
pkg/compose/stop.go

@@ -18,11 +18,11 @@ package compose
 
 import (
 	"context"
+	"slices"
 	"strings"
 
 	"github.com/docker/compose/v2/pkg/api"
 	"github.com/docker/compose/v2/pkg/progress"
-	"github.com/docker/compose/v2/pkg/utils"
 )
 
 func (s *composeService) Stop(ctx context.Context, projectName string, options api.StopOptions) error {
@@ -51,7 +51,7 @@ func (s *composeService) stop(ctx context.Context, projectName string, options a
 
 	w := progress.ContextWriter(ctx)
 	return InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
-		if !utils.StringContains(options.Services, service) {
+		if !slices.Contains(options.Services, service) {
 			return nil
 		}
 		serv := project.Services[service]

+ 2 - 2
pkg/progress/tty.go

@@ -20,12 +20,12 @@ import (
 	"context"
 	"fmt"
 	"io"
+	"slices"
 	"strings"
 	"sync"
 	"time"
 
 	"github.com/docker/compose/v2/pkg/api"
-	"github.com/docker/compose/v2/pkg/utils"
 
 	"github.com/buger/goterm"
 	"github.com/docker/go-units"
@@ -77,7 +77,7 @@ func (w *ttyWriter) Event(e Event) {
 }
 
 func (w *ttyWriter) event(e Event) {
-	if !utils.StringContains(w.eventIDs, e.ID) {
+	if !slices.Contains(w.eventIDs, e.ID) {
 		w.eventIDs = append(w.eventIDs, e.ID)
 	}
 	if _, ok := w.events[e.ID]; ok {

+ 0 - 10
pkg/utils/stringutils.go

@@ -21,16 +21,6 @@ import (
 	"strings"
 )
 
-// StringContains check if an array contains a specific value
-func StringContains(array []string, needle string) bool {
-	for _, val := range array {
-		if val == needle {
-			return true
-		}
-	}
-	return false
-}
-
 // StringToBool converts a string to a boolean ignoring errors
 func StringToBool(s string) bool {
 	s = strings.ToLower(strings.TrimSpace(s))