refactor: use slices.Contains to simplify code

Signed-off-by: tongjicoder <tongjicoder@icloud.com>
This commit is contained in:
tongjicoder
2025-05-27 14:43:50 +08:00
committed by Nicolas De loof
parent d49a68ecbf
commit 2e71440bee
14 changed files with 32 additions and 47 deletions

View File

@@ -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)
}
}