From ef0a7dd4861a1b191b5565c82ec033b1a7d38560 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 29 Sep 2025 22:17:51 -0700 Subject: [PATCH] pkg/api/handlers/compat: use strings.CutPrefix This way we don't check the string twice. Signed-off-by: Kir Kolyshkin --- pkg/api/handlers/compat/images_build.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/pkg/api/handlers/compat/images_build.go b/pkg/api/handlers/compat/images_build.go index f151dd1699..7e0ec54970 100644 --- a/pkg/api/handlers/compat/images_build.go +++ b/pkg/api/handlers/compat/images_build.go @@ -923,10 +923,8 @@ func handleBuildContexts(r *http.Request, query url.Values, anchorDir string, mu logrus.Debugf("name: %q, context: %q", name, value) - switch { - case strings.HasPrefix(value, "url:"): - value = strings.TrimPrefix(value, "url:") - tempDir, subdir, err := buildahDefine.TempDirForURL(anchorDir, "buildah", value) + if urlValue, ok := strings.CutPrefix(value, "url:"); ok { + tempDir, subdir, err := buildahDefine.TempDirForURL(anchorDir, "buildah", urlValue) if err != nil { return nil, fmt.Errorf("downloading URL %q: %w", name, err) } @@ -940,15 +938,14 @@ func handleBuildContexts(r *http.Request, query url.Values, anchorDir string, mu } logrus.Debugf("Downloaded URL context %q to %q", name, contextPath) - case strings.HasPrefix(value, "image:"): - value = strings.TrimPrefix(value, "image:") + } else if imageValue, ok := strings.CutPrefix(value, "image:"); ok { out.AdditionalBuildContexts[name] = &buildahDefine.AdditionalBuildContext{ IsURL: false, IsImage: true, - Value: value, + Value: imageValue, } - logrus.Debugf("Using image context %q: %q", name, value) + logrus.Debugf("Using image context %q: %q", name, imageValue) } } @@ -979,8 +976,7 @@ func handleBuildContexts(r *http.Request, query url.Values, anchorDir string, mu fieldName := part.FormName() - switch { - case fieldName == "MainContext": + if fieldName == "MainContext" { mainDir, err := extractTarFile(anchorDir, part) if err != nil { return nil, fmt.Errorf("extracting main context in multipart: %w", err) @@ -989,10 +985,7 @@ func handleBuildContexts(r *http.Request, query url.Values, anchorDir string, mu return nil, fmt.Errorf("main context directory is empty") } out.ContextDirectory = mainDir - - case strings.HasPrefix(fieldName, "build-context-"): - contextName := strings.TrimPrefix(fieldName, "build-context-") - + } else if contextName, ok := strings.CutPrefix(fieldName, "build-context-"); ok { // Create temp directory directly under anchorDir additionalAnchor, err := os.MkdirTemp(anchorDir, contextName+"-*") if err != nil { @@ -1039,7 +1032,7 @@ func handleBuildContexts(r *http.Request, query url.Values, anchorDir string, mu IsImage: false, Value: additionalAnchor, } - default: + } else { logrus.Debugf("Ignoring unknown multipart field: %s", fieldName) } }