summaryrefslogtreecommitdiff
path: root/Sources/BubombCore/Utilities/PathValidator.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Sources/BubombCore/Utilities/PathValidator.swift')
-rw-r--r--Sources/BubombCore/Utilities/PathValidator.swift69
1 files changed, 69 insertions, 0 deletions
diff --git a/Sources/BubombCore/Utilities/PathValidator.swift b/Sources/BubombCore/Utilities/PathValidator.swift
new file mode 100644
index 0000000..af8c14c
--- /dev/null
+++ b/Sources/BubombCore/Utilities/PathValidator.swift
@@ -0,0 +1,69 @@
+import Foundation
+
+/// Utilities for validating filesystem paths and URLs.
+public enum PathValidator {
+ /// Checks whether an executable exists at the given path and is executable.
+ public static func validateExecutable(at path: String) -> Result<String, ExecutionError> {
+ let expanded = NSString(string: path).expandingTildeInPath
+ let fileManager = FileManager.default
+
+ guard fileManager.fileExists(atPath: expanded) else {
+ return .failure(.executableNotFound(expanded))
+ }
+ guard fileManager.isExecutableFile(atPath: expanded) else {
+ return .failure(.permissionDenied("File is not executable: \(expanded)"))
+ }
+ return .success(expanded)
+ }
+
+ /// Checks whether a script file exists at the given path.
+ public static func validateScript(at path: String) -> Result<String, ExecutionError> {
+ let expanded = NSString(string: path).expandingTildeInPath
+ let fileManager = FileManager.default
+
+ guard fileManager.fileExists(atPath: expanded) else {
+ return .failure(.scriptNotFound(expanded))
+ }
+ guard fileManager.isReadableFile(atPath: expanded) else {
+ return .failure(.permissionDenied("Script file is not readable: \(expanded)"))
+ }
+ return .success(expanded)
+ }
+
+ /// Checks whether a working directory exists and is a directory.
+ public static func validateWorkingDirectory(_ path: String?) -> Result<String?, ExecutionError> {
+ guard let path = path, !path.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
+ return .success(nil)
+ }
+ let expanded = NSString(string: path).expandingTildeInPath
+ var isDir: ObjCBool = false
+ if FileManager.default.fileExists(atPath: expanded, isDirectory: &isDir), isDir.boolValue {
+ return .success(expanded)
+ }
+ return .failure(.workingDirectoryNotFound(expanded))
+ }
+
+ /// Validates an HTTP or HTTPS URL.
+ public static func validateURL(_ urlString: String) -> Result<URL, ExecutionError> {
+ let trimmed = urlString.trimmingCharacters(in: .whitespacesAndNewlines)
+ guard let url = URL(string: trimmed),
+ let scheme = url.scheme?.lowercased(),
+ (scheme == "http" || scheme == "https"),
+ url.host != nil,
+ !(url.host?.isEmpty ?? true) else {
+ return .failure(.invalidURL(urlString))
+ }
+ return .success(url)
+ }
+
+ /// Checks if an application bundle exists.
+ public static func validateApplication(at path: String) -> Result<String, ExecutionError> {
+ let expanded = NSString(string: path).expandingTildeInPath
+ let fileManager = FileManager.default
+
+ guard fileManager.fileExists(atPath: expanded) else {
+ return .failure(.applicationNotFound(expanded))
+ }
+ return .success(expanded)
+ }
+}