diff --git a/cmd/compose/images.go b/cmd/compose/images.go index d40367324..f06f8784c 100644 --- a/cmd/compose/images.go +++ b/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) } } diff --git a/cmd/compose/options.go b/cmd/compose/options.go index 0df72cd85..9e879fb07 100644 --- a/cmd/compose/options.go +++ b/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) } } diff --git a/cmd/compose/ps.go b/cmd/compose/ps.go index 7db4fdfd3..f393b2c50 100644 --- a/cmd/compose/ps.go +++ b/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) } } diff --git a/internal/ocipush/push.go b/internal/ocipush/push.go index a42bba568..c1bf58d64 100644 --- a/internal/ocipush/push.go +++ b/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) { diff --git a/pkg/api/api.go b/pkg/api/api.go index d4d540be2..16b1f9344 100644 --- a/pkg/api/api.go +++ b/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) } } diff --git a/pkg/compose/containers.go b/pkg/compose/containers.go index 1bf078432..ebf70d013 100644 --- a/pkg/compose/containers.go +++ b/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) } } diff --git a/pkg/compose/convergence.go b/pkg/compose/convergence.go index 90bf2ce01..e19866790 100644 --- a/pkg/compose/convergence.go +++ b/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) diff --git a/pkg/compose/dependencies.go b/pkg/compose/dependencies.go index ba8bb960a..19660e8e8 100644 --- a/pkg/compose/dependencies.go +++ b/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 } diff --git a/pkg/compose/events.go b/pkg/compose/events.go index 3d3f63104..4d6b913c4 100644 --- a/pkg/compose/events.go +++ b/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 } diff --git a/pkg/compose/images.go b/pkg/compose/images.go index c7bbaff41..1ce780942 100644 --- a/pkg/compose/images.go +++ b/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) } } diff --git a/pkg/compose/ls.go b/pkg/compose/ls.go index 34d3ceb0e..403b061a8 100644 --- a/pkg/compose/ls.go +++ b/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) } } diff --git a/pkg/compose/stop.go b/pkg/compose/stop.go index 34a1446a8..b9514c4b4 100644 --- a/pkg/compose/stop.go +++ b/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] diff --git a/pkg/progress/tty.go b/pkg/progress/tty.go index 7c65ad6d3..e6b8738c8 100644 --- a/pkg/progress/tty.go +++ b/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 { diff --git a/pkg/utils/stringutils.go b/pkg/utils/stringutils.go index 1004263bb..7135e9178 100644 --- a/pkg/utils/stringutils.go +++ b/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))