Przeglądaj źródła

replace some uses of strings.Split(N) for strings.Cut

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Sebastiaan van Stijn 3 tygodni temu
rodzic
commit
c51b1fea29

+ 3 - 4
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
 		}

+ 3 - 3
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

+ 6 - 6
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 {

+ 3 - 3
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

+ 7 - 10
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))
 	}

+ 3 - 3
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) {

+ 6 - 9
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,
 		}
 	}