pkg/api/handlers/compat: use strings.CutPrefix

This way we don't check the string twice.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2025-09-29 22:17:51 -07:00
parent f198fe6b13
commit ef0a7dd486

View File

@ -923,10 +923,8 @@ func handleBuildContexts(r *http.Request, query url.Values, anchorDir string, mu
logrus.Debugf("name: %q, context: %q", name, value) logrus.Debugf("name: %q, context: %q", name, value)
switch { if urlValue, ok := strings.CutPrefix(value, "url:"); ok {
case strings.HasPrefix(value, "url:"): tempDir, subdir, err := buildahDefine.TempDirForURL(anchorDir, "buildah", urlValue)
value = strings.TrimPrefix(value, "url:")
tempDir, subdir, err := buildahDefine.TempDirForURL(anchorDir, "buildah", value)
if err != nil { if err != nil {
return nil, fmt.Errorf("downloading URL %q: %w", name, err) 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) logrus.Debugf("Downloaded URL context %q to %q", name, contextPath)
case strings.HasPrefix(value, "image:"): } else if imageValue, ok := strings.CutPrefix(value, "image:"); ok {
value = strings.TrimPrefix(value, "image:")
out.AdditionalBuildContexts[name] = &buildahDefine.AdditionalBuildContext{ out.AdditionalBuildContexts[name] = &buildahDefine.AdditionalBuildContext{
IsURL: false, IsURL: false,
IsImage: true, 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() fieldName := part.FormName()
switch { if fieldName == "MainContext" {
case fieldName == "MainContext":
mainDir, err := extractTarFile(anchorDir, part) mainDir, err := extractTarFile(anchorDir, part)
if err != nil { if err != nil {
return nil, fmt.Errorf("extracting main context in multipart: %w", err) 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") return nil, fmt.Errorf("main context directory is empty")
} }
out.ContextDirectory = mainDir out.ContextDirectory = mainDir
} else if contextName, ok := strings.CutPrefix(fieldName, "build-context-"); ok {
case strings.HasPrefix(fieldName, "build-context-"):
contextName := strings.TrimPrefix(fieldName, "build-context-")
// Create temp directory directly under anchorDir // Create temp directory directly under anchorDir
additionalAnchor, err := os.MkdirTemp(anchorDir, contextName+"-*") additionalAnchor, err := os.MkdirTemp(anchorDir, contextName+"-*")
if err != nil { if err != nil {
@ -1039,7 +1032,7 @@ func handleBuildContexts(r *http.Request, query url.Values, anchorDir string, mu
IsImage: false, IsImage: false,
Value: additionalAnchor, Value: additionalAnchor,
} }
default: } else {
logrus.Debugf("Ignoring unknown multipart field: %s", fieldName) logrus.Debugf("Ignoring unknown multipart field: %s", fieldName)
} }
} }