modernize: Use strings.CutSuffix

Added in Go 1.20

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2025-12-15 18:32:55 +01:00
parent 71fd582aa2
commit 6c5233e109
6 changed files with 15 additions and 15 deletions

View File

@@ -634,8 +634,8 @@ func setupLabelFilter(ctx context.Context, store content.Store, fltrs filters.Ar
negate := strings.HasSuffix(fltrName, "!")
// If filter value is key!=value then flip the above.
if strings.HasSuffix(k, "!") {
k = strings.TrimSuffix(k, "!")
if before, ok := strings.CutSuffix(k, "!"); ok {
k = before
negate = !negate
}

View File

@@ -403,7 +403,7 @@ func extractDistributionSources(labels map[string]string) []distributionSource {
// Check if this blob has a distributionSource label
// if yes, read it as source
for k, v := range labels {
if reg := strings.TrimPrefix(k, containerdlabels.LabelDistributionSource); reg != k {
if reg, ok := strings.CutPrefix(k, containerdlabels.LabelDistributionSource); ok {
for repo := range strings.SplitSeq(v, ",") {
ref, err := reference.ParseNamed(reg + "/" + repo)
if err != nil {

View File

@@ -343,8 +343,8 @@ func parseInitVersion(v string) (version string, commit string, _ error) {
}
}
parts[0] = strings.TrimSpace(parts[0])
if strings.HasPrefix(parts[0], "tini version ") {
version = strings.TrimPrefix(parts[0], "tini version ")
if after, ok := strings.CutPrefix(parts[0], "tini version "); ok {
version = after
}
if version == "" && commit == "" {
return "", "", errors.Errorf("unknown output format: %s", v)
@@ -369,8 +369,8 @@ func parseRuntimeVersion(v string) (runtime, version, commit string, _ error) {
version = strings.TrimSpace(s[len(s)-1])
continue
}
if strings.HasPrefix(line, "commit:") {
commit = strings.TrimSpace(strings.TrimPrefix(line, "commit:"))
if after, ok := strings.CutPrefix(line, "commit:"); ok {
commit = strings.TrimSpace(after)
continue
}
}

View File

@@ -76,8 +76,8 @@ func GenerateKey(containerID string) string {
for _, v := range dir {
id := v.Name()
if strings.HasSuffix(id, containerID[:maxLen-1]) {
indexStr = strings.TrimSuffix(id, containerID[:maxLen-1])
if before, ok := strings.CutSuffix(id, containerID[:maxLen-1]); ok {
indexStr = before
tmpindex, err := strconv.Atoi(indexStr)
if err != nil {
return ""

View File

@@ -420,8 +420,8 @@ func (v *localVolume) LiveRestoreVolume(ctx context.Context, _ string) error {
// getAddress finds out address/hostname from options
func getAddress(opts string) string {
for opt := range strings.SplitSeq(opts, ",") {
if strings.HasPrefix(opt, "addr=") {
return strings.TrimPrefix(opt, "addr=")
if after, ok := strings.CutPrefix(opt, "addr="); ok {
return after
}
}
return ""
@@ -430,8 +430,8 @@ func getAddress(opts string) string {
// getPassword finds out a password from options
func getPassword(opts string) string {
for opt := range strings.SplitSeq(opts, ",") {
if strings.HasPrefix(opt, "password=") {
return strings.TrimPrefix(opt, "password=")
if after, ok := strings.CutPrefix(opt, "password="); ok {
return after
}
}
return ""

View File

@@ -56,8 +56,8 @@ func getValueFromOsRelease(key string) (string, error) {
scanner := bufio.NewScanner(osReleaseFile)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, key+"=") {
value = strings.TrimPrefix(line, key+"=")
if after, ok := strings.CutPrefix(line, key+"="); ok {
value = after
value = strings.Trim(value, `"' `) // remove leading/trailing quotes and whitespace
}
}