chore: use strings.Cut

This commit is contained in:
Julian Sommer
2026-09-30 11:48:27 +10:00
parent 610ee9bab0
commit 810aa019bf
7 changed files with 14 additions and 26 deletions
+3 -3
View File
@@ -3499,11 +3499,11 @@ func resolvePublicPoolFlags(source, poolRange, bindBridge, gateway, defaultBindB
if gateway == "" {
return "", "", "", "", fmt.Errorf("--external-gateway is required with --external-source=static")
}
parts := strings.SplitN(poolRange, "-", 2)
if len(parts) != 2 || net.ParseIP(parts[0]) == nil || net.ParseIP(parts[1]) == nil {
var ok bool
start, end, ok = strings.Cut(poolRange, "-")
if !ok || net.ParseIP(start) == nil || net.ParseIP(end) == nil {
return "", "", "", "", fmt.Errorf("--external-pool must be start-end IPs (e.g., 192.168.1.150-192.168.1.250), got: %s", poolRange)
}
start, end = parts[0], parts[1]
default:
return "", "", "", "", fmt.Errorf("--external-source must be 'static' or 'dhcp', got: %s", source)
}
+1 -5
View File
@@ -438,14 +438,10 @@ func parseWeightsS3URI(uri string) (bucket, prefix string, err error) {
if trimmed == uri {
return "", "", fmt.Errorf("invalid --s3-uri %q: expected s3://bucket/prefix", uri)
}
parts := strings.SplitN(trimmed, "/", 2)
bucket = parts[0]
bucket, prefix, _ = strings.Cut(trimmed, "/")
if bucket == "" {
return "", "", fmt.Errorf("invalid --s3-uri %q: missing bucket", uri)
}
if len(parts) == 2 {
prefix = parts[1]
}
if prefix != "" && !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
+1 -3
View File
@@ -114,9 +114,7 @@ func (m *Model) decodeErrorCode(body []byte) (string, error) {
if separator := strings.LastIndex(code, "#"); separator >= 0 {
code = code[separator+1:]
}
if separator := strings.Index(code, ":"); separator >= 0 {
code = code[:separator]
}
code, _, _ = strings.Cut(code, ":")
if code == "" {
return "", fmt.Errorf("JSON error envelope has no code")
}
+1 -3
View File
@@ -135,9 +135,7 @@ func DecodeRequestResult(service Service, status int, header http.Header, body [
}
func trimErrorCode(code string) string {
if separator := strings.Index(code, ":"); separator >= 0 {
code = code[:separator]
}
code, _, _ = strings.Cut(code, ":")
if separator := strings.LastIndex(code, "#"); separator >= 0 {
code = code[separator+1:]
}
+4 -6
View File
@@ -109,11 +109,9 @@ func GatewayHTTPClient(caPath string) (*http.Client, error) {
// HostFromImage returns the host[:port] portion of an image ref or URL. It strips
// any scheme and trims at the first path separator.
func HostFromImage(image string) string {
if i := strings.Index(image, "://"); i >= 0 {
image = image[i+3:]
if _, rest, ok := strings.Cut(image, "://"); ok {
image = rest
}
if i := strings.IndexByte(image, '/'); i >= 0 {
image = image[:i]
}
return image
host, _, _ := strings.Cut(image, "/")
return host
}
+1 -3
View File
@@ -42,9 +42,7 @@ func (r *TOMLVersionReader) ReadVersion(path string) (int, error) {
}
// Handle "1.0" style — take the integer part before the dot.
if idx := strings.Index(raw, "."); idx >= 0 {
raw = raw[:idx]
}
raw, _, _ = strings.Cut(raw, ".")
v, err := strconv.Atoi(raw)
if err != nil {
+3 -3
View File
@@ -65,11 +65,11 @@ func DiscoverNanoInstanceType(t *testing.T, fx *Fixture) (instanceType, arch str
if err != nil {
t.Fatalf("DiscoverNanoInstanceType: %v", err)
}
parts := strings.SplitN(combined, "|", 2)
if len(parts) != 2 {
instanceType, arch, ok := strings.Cut(combined, "|")
if !ok {
t.Fatalf("DiscoverNanoInstanceType: malformed memo value %q", combined)
}
return parts[0], parts[1]
return instanceType, arch
}
// DiscoverUbuntuAMI returns the AMI ID for the architecture-appropriate Ubuntu