From 99082fd8c3bb76c39ca64af33ca074c9a7c80938 Mon Sep 17 00:00:00 2001 From: Zhan Ziyang Date: Sat, 22 Aug 2026 15:53:31 +0800 Subject: [PATCH] refactor: simplify optional container path validation --- internal/backendexecutor/executor.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/internal/backendexecutor/executor.go b/internal/backendexecutor/executor.go index 8e98e76..c7dd2e0 100644 --- a/internal/backendexecutor/executor.go +++ b/internal/backendexecutor/executor.go @@ -355,15 +355,11 @@ func validateRequest(request Request) error { if request.PortEnvironmentKey == "" || strings.Contains(request.PortEnvironmentKey, "=") || strings.TrimSpace(request.PortEnvironmentKey) != request.PortEnvironmentKey { return errors.New("exact port environment key is required") } - if request.ConfigSource != "" || request.ConfigTarget != "" { - if !filepath.IsAbs(request.ConfigSource) || !filepath.IsAbs(request.ConfigTarget) { - return errors.New("backend configuration source and target must be absolute paths") - } + if err := validateOptionalPathPair(request.ConfigSource, request.ConfigTarget, "backend configuration source and target"); err != nil { + return err } - if request.TmpSource != "" || request.TmpTarget != "" { - if !filepath.IsAbs(request.TmpSource) || !filepath.IsAbs(request.TmpTarget) { - return errors.New("backend temporary source and target must be absolute paths") - } + if err := validateOptionalPathPair(request.TmpSource, request.TmpTarget, "backend temporary source and target"); err != nil { + return err } if request.ConfigSource != "" { if request.ConfigEnvironmentKey == "" || strings.Contains(request.ConfigEnvironmentKey, "=") || strings.TrimSpace(request.ConfigEnvironmentKey) != request.ConfigEnvironmentKey { @@ -432,6 +428,17 @@ func directDirectory(path, description string) error { return nil } +// validateOptionalPathPair accepts two empty paths or two clean absolute paths. +func validateOptionalPathPair(source string, target string, description string) error { + if source == "" && target == "" { + return nil + } + if !filepath.IsAbs(source) || !filepath.IsAbs(target) { + return fmt.Errorf("%s must be absolute paths", description) + } + return nil +} + // containerSpec 根据请求构造后端容器的完整规格,包括名称、镜像引用、平台、环境变量、 // 宿主机网络模式、重启策略、绑定挂载、用户与停止超时。 func containerSpec(request Request) containerengine.ContainerSpec {