refactor: simplify optional container path validation
This commit is contained in:
@@ -355,15 +355,11 @@ func validateRequest(request Request) error {
|
|||||||
if request.PortEnvironmentKey == "" || strings.Contains(request.PortEnvironmentKey, "=") || strings.TrimSpace(request.PortEnvironmentKey) != request.PortEnvironmentKey {
|
if request.PortEnvironmentKey == "" || strings.Contains(request.PortEnvironmentKey, "=") || strings.TrimSpace(request.PortEnvironmentKey) != request.PortEnvironmentKey {
|
||||||
return errors.New("exact port environment key is required")
|
return errors.New("exact port environment key is required")
|
||||||
}
|
}
|
||||||
if request.ConfigSource != "" || request.ConfigTarget != "" {
|
if err := validateOptionalPathPair(request.ConfigSource, request.ConfigTarget, "backend configuration source and target"); err != nil {
|
||||||
if !filepath.IsAbs(request.ConfigSource) || !filepath.IsAbs(request.ConfigTarget) {
|
return err
|
||||||
return errors.New("backend configuration source and target must be absolute paths")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if request.TmpSource != "" || request.TmpTarget != "" {
|
if err := validateOptionalPathPair(request.TmpSource, request.TmpTarget, "backend temporary source and target"); err != nil {
|
||||||
if !filepath.IsAbs(request.TmpSource) || !filepath.IsAbs(request.TmpTarget) {
|
return err
|
||||||
return errors.New("backend temporary source and target must be absolute paths")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if request.ConfigSource != "" {
|
if request.ConfigSource != "" {
|
||||||
if request.ConfigEnvironmentKey == "" || strings.Contains(request.ConfigEnvironmentKey, "=") || strings.TrimSpace(request.ConfigEnvironmentKey) != request.ConfigEnvironmentKey {
|
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
|
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 根据请求构造后端容器的完整规格,包括名称、镜像引用、平台、环境变量、
|
// containerSpec 根据请求构造后端容器的完整规格,包括名称、镜像引用、平台、环境变量、
|
||||||
// 宿主机网络模式、重启策略、绑定挂载、用户与停止超时。
|
// 宿主机网络模式、重启策略、绑定挂载、用户与停止超时。
|
||||||
func containerSpec(request Request) containerengine.ContainerSpec {
|
func containerSpec(request Request) containerengine.ContainerSpec {
|
||||||
|
|||||||
Reference in New Issue
Block a user