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, "!") negate := strings.HasSuffix(fltrName, "!")
// If filter value is key!=value then flip the above. // If filter value is key!=value then flip the above.
if strings.HasSuffix(k, "!") { if before, ok := strings.CutSuffix(k, "!"); ok {
k = strings.TrimSuffix(k, "!") k = before
negate = !negate negate = !negate
} }

View File

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

View File

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

View File

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

View File

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

View File

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