mirror of
https://github.com/docker/compose.git
synced 2026-08-04 06:38:45 +00:00
refactor: use slices.Contains to simplify code
Signed-off-by: tongjicoder <tongjicoder@icloud.com>
This commit is contained in:
parent
d49a68ecbf
commit
2e71440bee
14 changed files with 32 additions and 47 deletions
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue