Browse code

daemon/cluster: use strings.Cut()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2022/10/31 23:01:02
Showing 2 changed files
... ...
@@ -637,18 +637,18 @@ func parsePortMap(portMap nat.PortMap) ([]*api.PortConfig, error) {
637 637
 	exposedPorts := make([]*api.PortConfig, 0, len(portMap))
638 638
 
639 639
 	for portProtocol, mapping := range portMap {
640
-		parts := strings.SplitN(string(portProtocol), "/", 2)
641
-		if len(parts) != 2 {
640
+		p, proto, ok := strings.Cut(string(portProtocol), "/")
641
+		if !ok {
642 642
 			return nil, fmt.Errorf("invalid port mapping: %s", portProtocol)
643 643
 		}
644 644
 
645
-		port, err := strconv.ParseUint(parts[0], 10, 16)
645
+		port, err := strconv.ParseUint(p, 10, 16)
646 646
 		if err != nil {
647 647
 			return nil, err
648 648
 		}
649 649
 
650 650
 		var protocol api.PortConfig_Protocol
651
-		switch strings.ToLower(parts[1]) {
651
+		switch strings.ToLower(proto) {
652 652
 		case "tcp":
653 653
 			protocol = api.ProtocolTCP
654 654
 		case "udp":
... ...
@@ -656,7 +656,7 @@ func parsePortMap(portMap nat.PortMap) ([]*api.PortConfig, error) {
656 656
 		case "sctp":
657 657
 			protocol = api.ProtocolSCTP
658 658
 		default:
659
-			return nil, fmt.Errorf("invalid protocol: %s", parts[1])
659
+			return nil, fmt.Errorf("invalid protocol: %s", proto)
660 660
 		}
661 661
 
662 662
 		for _, binding := range mapping {
... ...
@@ -114,11 +114,11 @@ func (e *executor) Describe(ctx context.Context) (*api.NodeDescription, error) {
114 114
 	// parse []string labels into a map[string]string
115 115
 	labels := map[string]string{}
116 116
 	for _, l := range info.Labels {
117
-		stringSlice := strings.SplitN(l, "=", 2)
117
+		k, v, ok := strings.Cut(l, "=")
118 118
 		// this will take the last value in the list for a given key
119 119
 		// ideally, one shouldn't assign multiple values to the same key
120
-		if len(stringSlice) > 1 {
121
-			labels[stringSlice[0]] = stringSlice[1]
120
+		if ok {
121
+			labels[k] = v
122 122
 		}
123 123
 	}
124 124