summaryrefslogtreecommitdiff
path: root/Sources/BubombCore/Utilities
diff options
context:
space:
mode:
authorgigagamerchaddhruv-spec <gigagamerchaddhruv-spec@users.noreply.github.com>2026-10-14 20:18:20 +0530
committergigagamerchaddhruv-spec <gigagamerchaddhruv-spec@users.noreply.github.com>2026-10-14 20:18:20 +0530
commitaceca8b5dfc8edd378ee9ff891bccb0f94c69f31 (patch)
tree08401ce8d050afbbe77514f479601a8e9535f4ec /Sources/BubombCore/Utilities
feat: launch Bubomb - native macOS menu bar utility ('Configure the boom')HEADmain
Diffstat (limited to 'Sources/BubombCore/Utilities')
-rw-r--r--Sources/BubombCore/Utilities/ExecutionError.swift64
-rw-r--r--Sources/BubombCore/Utilities/PathValidator.swift69
2 files changed, 133 insertions, 0 deletions
diff --git a/Sources/BubombCore/Utilities/ExecutionError.swift b/Sources/BubombCore/Utilities/ExecutionError.swift
new file mode 100644
index 0000000..ea2fd5f
--- /dev/null
+++ b/Sources/BubombCore/Utilities/ExecutionError.swift
@@ -0,0 +1,64 @@
+import Foundation
+
+/// Errors that can occur during Bubomb payload validation, execution, and persistence.
+public enum ExecutionError: LocalizedError, Equatable, Sendable {
+ case noPayloadConfigured
+ case invalidConfiguration(String)
+ case executableNotFound(String)
+ case scriptNotFound(String)
+ case workingDirectoryNotFound(String)
+ case invalidURL(String)
+ case permissionDenied(String)
+ case nonZeroExitStatus(Int, output: String)
+ case timeout(TimeInterval)
+ case cancelled
+ case processLaunchFailed(String)
+ case duplicateExecution
+ case destructiveActionRejected
+ case stepFailed(stepIndex: Int, stepName: String, reason: String)
+ case applicationNotFound(String)
+ case applicationLaunchFailed(String)
+
+ public var errorDescription: String? {
+ switch self {
+ case .noPayloadConfigured:
+ return "No payload configured. Open Settings to set up an action."
+ case .invalidConfiguration(let details):
+ return "Invalid configuration: \(details)"
+ case .executableNotFound(let path):
+ return "Executable not found at path: \(path)"
+ case .scriptNotFound(let path):
+ return "Script file not found at path: \(path)"
+ case .workingDirectoryNotFound(let path):
+ return "Working directory does not exist: \(path)"
+ case .invalidURL(let url):
+ return "Invalid URL: \(url). Only valid http:// and https:// URLs are supported."
+ case .permissionDenied(let path):
+ return "Permission denied accessing: \(path)"
+ case .nonZeroExitStatus(let code, let output):
+ let trimmed = output.trimmingCharacters(in: .whitespacesAndNewlines)
+ if trimmed.isEmpty {
+ return "Command failed with exit status \(code)."
+ } else {
+ let firstLine = trimmed.components(separatedBy: .newlines).first ?? ""
+ return "Command failed with exit status \(code): \(firstLine)"
+ }
+ case .timeout(let duration):
+ return "Execution timed out after \(String(format: "%.1f", duration)) seconds."
+ case .cancelled:
+ return "Execution was cancelled by the user."
+ case .processLaunchFailed(let details):
+ return "Failed to launch process: \(details)"
+ case .duplicateExecution:
+ return "A payload is already running. Wait for it to finish or cancel it."
+ case .destructiveActionRejected:
+ return "Destructive action was rejected by the user."
+ case .stepFailed(let index, let name, let reason):
+ return "Step \(index + 1) ('\(name)') failed: \(reason)"
+ case .applicationNotFound(let path):
+ return "Application not found at path: \(path)"
+ case .applicationLaunchFailed(let details):
+ return "Failed to open application: \(details)"
+ }
+ }
+}
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)
+ }
+}