From a59e39ea962a2a2cd1bdb5b2b664c3e4efbe9c85 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 22 Jan 2026 11:07:29 +0100 Subject: [PATCH] replace some uses of strings.Split(N) for strings.Cut Signed-off-by: Sebastiaan van Stijn --- cmd/compose/create.go | 7 +++---- cmd/compose/options.go | 6 +++--- cmd/compose/ps.go | 12 ++++++------ cmd/compose/run.go | 6 +++--- pkg/compose/convergence.go | 17 +++++++---------- pkg/compose/cp.go | 6 +++--- pkg/compose/create.go | 15 ++++++--------- 7 files changed, 31 insertions(+), 38 deletions(-) diff --git a/cmd/compose/create.go b/cmd/compose/create.go index fa6432c88c7..5f9f7908315 100644 --- a/cmd/compose/create.go +++ b/cmd/compose/create.go @@ -198,12 +198,11 @@ func (opts createOptions) Apply(project *types.Project) error { func applyScaleOpts(project *types.Project, opts []string) error { for _, scale := range opts { - split := strings.Split(scale, "=") - if len(split) != 2 { + name, val, ok := strings.Cut(scale, "=") + if !ok || val == "" { return fmt.Errorf("invalid --scale option %q. Should be SERVICE=NUM", scale) } - name := split[0] - replicas, err := strconv.Atoi(split[1]) + replicas, err := strconv.Atoi(val) if err != nil { return err } diff --git a/cmd/compose/options.go b/cmd/compose/options.go index 2cab4742cae..6454f6aed9b 100644 --- a/cmd/compose/options.go +++ b/cmd/compose/options.go @@ -213,9 +213,9 @@ func extractEnvCLIDefined(cmdEnvs []string) map[string]string { // Parse command-line environment variables cmdEnvMap := make(map[string]string) for _, env := range cmdEnvs { - parts := strings.SplitN(env, "=", 2) - if len(parts) == 2 { - cmdEnvMap[parts[0]] = parts[1] + key, val, ok := strings.Cut(env, "=") + if ok { + cmdEnvMap[key] = val } } return cmdEnvMap diff --git a/cmd/compose/ps.go b/cmd/compose/ps.go index 37a39eef97f..878bb3186e1 100644 --- a/cmd/compose/ps.go +++ b/cmd/compose/ps.go @@ -50,19 +50,19 @@ func (p *psOptions) parseFilter() error { if p.Filter == "" { return nil } - parts := strings.SplitN(p.Filter, "=", 2) - if len(parts) != 2 { + key, val, ok := strings.Cut(p.Filter, "=") + if !ok { return errors.New("arguments to --filter should be in form KEY=VAL") } - switch parts[0] { + switch key { case "status": - p.Status = append(p.Status, parts[1]) + p.Status = append(p.Status, val) + return nil case "source": return api.ErrNotImplemented default: - return fmt.Errorf("unknown filter %s", parts[0]) + return fmt.Errorf("unknown filter %s", key) } - return nil } func psCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command { diff --git a/cmd/compose/run.go b/cmd/compose/run.go index c18fdf23758..573c0e14d4f 100644 --- a/cmd/compose/run.go +++ b/cmd/compose/run.go @@ -284,11 +284,11 @@ func runRun(ctx context.Context, backend api.Compose, project *types.Project, op labels := types.Labels{} for _, s := range options.labels { - parts := strings.SplitN(s, "=", 2) - if len(parts) != 2 { + key, val, ok := strings.Cut(s, "=") + if !ok { return fmt.Errorf("label must be set as KEY=VALUE") } - labels[parts[0]] = parts[1] + labels[key] = val } var buildForRun *api.BuildOptions diff --git a/pkg/compose/convergence.go b/pkg/compose/convergence.go index 3644c9cf356..baf34a227f7 100644 --- a/pkg/compose/convergence.go +++ b/pkg/compose/convergence.go @@ -774,11 +774,10 @@ func (s *composeService) getLinks(ctx context.Context, projectName string, servi } for _, rawLink := range service.Links { - linkSplit := strings.Split(rawLink, ":") - linkServiceName := linkSplit[0] - linkName := linkServiceName - if len(linkSplit) == 2 { - linkName = linkSplit[1] // linkName if informed like in: "serviceName:linkName" + // linkName if informed like in: "serviceName[:linkName]" + linkServiceName, linkName, ok := strings.Cut(rawLink, ":") + if !ok { + linkName = linkServiceName } cnts, err := getServiceContainers(linkServiceName) if err != nil { @@ -810,11 +809,9 @@ func (s *composeService) getLinks(ctx context.Context, projectName string, servi } for _, rawExtLink := range service.ExternalLinks { - extLinkSplit := strings.Split(rawExtLink, ":") - externalLink := extLinkSplit[0] - linkName := externalLink - if len(extLinkSplit) == 2 { - linkName = extLinkSplit[1] + externalLink, linkName, ok := strings.Cut(rawExtLink, ":") + if !ok { + linkName = externalLink } links = append(links, format(externalLink, linkName)) } diff --git a/pkg/compose/cp.go b/pkg/compose/cp.go index 66c4917e515..f912c0827af 100644 --- a/pkg/compose/cp.go +++ b/pkg/compose/cp.go @@ -317,15 +317,15 @@ func splitCpArg(arg string) (ctr, path string) { return "", arg } - parts := strings.SplitN(arg, ":", 2) + ctr, path, ok := strings.Cut(arg, ":") - if len(parts) == 1 || strings.HasPrefix(parts[0], ".") { + if !ok || strings.HasPrefix(ctr, ".") { // Either there's no `:` in the arg // OR it's an explicit local relative path like `./file:name.txt`. return "", arg } - return parts[0], parts[1] + return ctr, path } func resolveLocalPath(localPath string) (absPath string, err error) { diff --git a/pkg/compose/create.go b/pkg/compose/create.go index e4293cefbd5..f01c3e9cec4 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -241,11 +241,8 @@ func (s *composeService) getCreateConfigs(ctx context.Context, } // VOLUMES/MOUNTS/FILESYSTEMS tmpfs := map[string]string{} for _, t := range service.Tmpfs { - if arr := strings.SplitN(t, ":", 2); len(arr) > 1 { - tmpfs[arr[0]] = arr[1] - } else { - tmpfs[arr[0]] = "" - } + k, v, _ := strings.Cut(t, ":") + tmpfs[k] = v } binds, mounts, err := s.buildContainerVolumes(ctx, *p, service, inherit) if err != nil { @@ -563,13 +560,13 @@ func defaultNetworkSettings(project *types.Project, func getRestartPolicy(service types.ServiceConfig) container.RestartPolicy { var restart container.RestartPolicy if service.Restart != "" { - split := strings.Split(service.Restart, ":") + name, num, ok := strings.Cut(service.Restart, ":") var attempts int - if len(split) > 1 { - attempts, _ = strconv.Atoi(split[1]) + if ok { + attempts, _ = strconv.Atoi(num) } restart = container.RestartPolicy{ - Name: mapRestartPolicyCondition(split[0]), + Name: mapRestartPolicyCondition(name), MaximumRetryCount: attempts, } }