refactor: use slices.Contains to simplify code
Signed-off-by: tongjicoder <tongjicoder@icloud.com>
This commit is contained in:
parent
d49a68ecbf
commit
2e71440bee
@ -20,6 +20,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -30,7 +31,6 @@ import (
|
|||||||
|
|
||||||
"github.com/docker/compose/v2/cmd/formatter"
|
"github.com/docker/compose/v2/cmd/formatter"
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"github.com/docker/compose/v2/pkg/api"
|
||||||
"github.com/docker/compose/v2/pkg/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type imageOptions struct {
|
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 {
|
if i := strings.IndexRune(img.ID, ':'); i >= 0 {
|
||||||
id = id[i+1:]
|
id = id[i+1:]
|
||||||
}
|
}
|
||||||
if !utils.StringContains(ids, id) {
|
if !slices.Contains(ids, id) {
|
||||||
ids = append(ids, id)
|
ids = append(ids, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
@ -32,7 +33,6 @@ import (
|
|||||||
"github.com/docker/compose/v2/internal/tracing"
|
"github.com/docker/compose/v2/internal/tracing"
|
||||||
ui "github.com/docker/compose/v2/pkg/progress"
|
ui "github.com/docker/compose/v2/pkg/progress"
|
||||||
"github.com/docker/compose/v2/pkg/prompt"
|
"github.com/docker/compose/v2/pkg/prompt"
|
||||||
"github.com/docker/compose/v2/pkg/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
|
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
|
// default platform only applies if the service doesn't specify
|
||||||
if defaultPlatform != "" && service.Platform == "" {
|
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)
|
return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", name, defaultPlatform)
|
||||||
}
|
}
|
||||||
service.Platform = defaultPlatform
|
service.Platform = defaultPlatform
|
||||||
@ -52,7 +52,7 @@ func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
|
|||||||
|
|
||||||
if service.Platform != "" {
|
if service.Platform != "" {
|
||||||
if len(service.Build.Platforms) > 0 {
|
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)
|
return fmt.Errorf("service %q build configuration does not support platform: %s", name, service.Platform)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,12 +20,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/compose/v2/cmd/formatter"
|
"github.com/docker/compose/v2/cmd/formatter"
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"github.com/docker/compose/v2/pkg/api"
|
||||||
"github.com/docker/compose/v2/pkg/utils"
|
|
||||||
|
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
cliformatter "github.com/docker/cli/cli/command/formatter"
|
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()
|
names := project.ServiceNames()
|
||||||
if len(services) > 0 {
|
if len(services) > 0 {
|
||||||
for _, service := range services {
|
for _, service := range services {
|
||||||
if !utils.StringContains(names, service) {
|
if !slices.Contains(names, service) {
|
||||||
return fmt.Errorf("no such service: %s", 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{}
|
services := []string{}
|
||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
s := c.Service
|
s := c.Service
|
||||||
if !utils.StringContains(services, s) {
|
if !slices.Contains(services, s) {
|
||||||
services = append(services, s)
|
services = append(services, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
pusherrors "github.com/containerd/containerd/v2/core/remotes/errors"
|
pusherrors "github.com/containerd/containerd/v2/core/remotes/errors"
|
||||||
@ -157,14 +158,7 @@ func isNonAuthClientError(statusCode int) bool {
|
|||||||
// not a client error
|
// not a client error
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, v := range clientAuthStatusCodes {
|
return !slices.Contains(clientAuthStatusCodes, statusCode)
|
||||||
if statusCode == v {
|
|
||||||
// client auth error
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// any other 4xx client error
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateManifest(layers []v1.Descriptor, ociCompat api.OCIVersion) ([]Pushable, error) {
|
func generateManifest(layers []v1.Descriptor, ociCompat api.OCIVersion) ([]Pushable, error) {
|
||||||
|
@ -19,12 +19,12 @@ package api
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/v2/types"
|
"github.com/compose-spec/compose-go/v2/types"
|
||||||
"github.com/docker/cli/opts"
|
"github.com/docker/cli/opts"
|
||||||
"github.com/docker/compose/v2/pkg/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Service manages a compose project
|
// Service manages a compose project
|
||||||
@ -175,13 +175,13 @@ func (o BuildOptions) Apply(project *types.Project) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if platform != "" {
|
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)
|
return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", name, platform)
|
||||||
}
|
}
|
||||||
service.Platform = platform
|
service.Platform = platform
|
||||||
}
|
}
|
||||||
if service.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)
|
return fmt.Errorf("service %q build configuration does not support platform: %s", name, service.Platform)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,12 +19,12 @@ package compose
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/v2/types"
|
"github.com/compose-spec/compose-go/v2/types"
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"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/container"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
)
|
)
|
||||||
@ -124,7 +124,7 @@ func matches(c container.Summary, predicates ...containerPredicate) bool {
|
|||||||
func isService(services ...string) containerPredicate {
|
func isService(services ...string) containerPredicate {
|
||||||
return func(c container.Summary) bool {
|
return func(c container.Summary) bool {
|
||||||
service := c.Labels[api.ServiceLabel]
|
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 that is not defined in the compose model
|
||||||
service := c.Labels[api.ServiceLabel]
|
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 {
|
return tracing.SpanWrapFunc("service/apply", tracing.ServiceOptions(service), func(ctx context.Context) error {
|
||||||
strategy := options.RecreateDependencies
|
strategy := options.RecreateDependencies
|
||||||
if utils.StringContains(options.Services, name) {
|
if slices.Contains(options.Services, name) {
|
||||||
strategy = options.Recreate
|
strategy = options.Recreate
|
||||||
}
|
}
|
||||||
return c.ensureService(ctx, project, service, strategy, options.Inherit, options.Timeout)
|
return c.ensureService(ctx, project, service, strategy, options.Inherit, options.Timeout)
|
||||||
|
@ -19,6 +19,7 @@ package compose
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -434,7 +435,7 @@ func (g *Graph) HasCycles() (bool, error) {
|
|||||||
path := []string{
|
path := []string{
|
||||||
vertex.Key,
|
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
|
var err error
|
||||||
discovered, finished, err = g.visit(vertex.Key, path, discovered, finished)
|
discovered, finished, err = g.visit(vertex.Key, path, discovered, finished)
|
||||||
if err != nil {
|
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 {
|
for _, v := range g.Vertices[key].Children {
|
||||||
path := append(path, v.Key)
|
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, " -> "))
|
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 {
|
if _, _, err := g.visit(v.Key, path, discovered, finished); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ package compose
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -25,7 +26,6 @@ import (
|
|||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
|
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"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 {
|
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
|
continue
|
||||||
}
|
}
|
||||||
service := event.Actor.Attributes[api.ServiceLabel]
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ package compose
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -29,7 +30,6 @@ import (
|
|||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
|
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"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) {
|
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 {
|
if len(options.Services) > 0 {
|
||||||
// filter service containers
|
// filter service containers
|
||||||
for _, c := range allContainers {
|
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)
|
containers = append(containers, c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,7 +55,7 @@ func (s *composeService) Images(ctx context.Context, projectName string, options
|
|||||||
|
|
||||||
images := []string{}
|
images := []string{}
|
||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
if !utils.StringContains(images, c.Image) {
|
if !slices.Contains(images, c.Image) {
|
||||||
images = append(images, c.Image)
|
images = append(images, c.Image)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,11 +19,11 @@ package compose
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"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/container"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -74,7 +74,7 @@ func combinedConfigFiles(containers []container.Summary) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, f := range strings.Split(files, ",") {
|
for _, f := range strings.Split(files, ",") {
|
||||||
if !utils.StringContains(configFiles, f) {
|
if !slices.Contains(configFiles, f) {
|
||||||
configFiles = append(configFiles, f)
|
configFiles = append(configFiles, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,11 +18,11 @@ package compose
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"github.com/docker/compose/v2/pkg/api"
|
||||||
"github.com/docker/compose/v2/pkg/progress"
|
"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 {
|
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)
|
w := progress.ContextWriter(ctx)
|
||||||
return InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
serv := project.Services[service]
|
serv := project.Services[service]
|
||||||
|
@ -20,12 +20,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"github.com/docker/compose/v2/pkg/api"
|
||||||
"github.com/docker/compose/v2/pkg/utils"
|
|
||||||
|
|
||||||
"github.com/buger/goterm"
|
"github.com/buger/goterm"
|
||||||
"github.com/docker/go-units"
|
"github.com/docker/go-units"
|
||||||
@ -77,7 +77,7 @@ func (w *ttyWriter) Event(e Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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)
|
w.eventIDs = append(w.eventIDs, e.ID)
|
||||||
}
|
}
|
||||||
if _, ok := w.events[e.ID]; ok {
|
if _, ok := w.events[e.ID]; ok {
|
||||||
|
@ -21,16 +21,6 @@ import (
|
|||||||
"strings"
|
"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
|
// StringToBool converts a string to a boolean ignoring errors
|
||||||
func StringToBool(s string) bool {
|
func StringToBool(s string) bool {
|
||||||
s = strings.ToLower(strings.TrimSpace(s))
|
s = strings.ToLower(strings.TrimSpace(s))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user