diff options
Diffstat (limited to 'Sources/BubombCore/Executors/URLLauncher.swift')
| -rw-r--r-- | Sources/BubombCore/Executors/URLLauncher.swift | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/Sources/BubombCore/Executors/URLLauncher.swift b/Sources/BubombCore/Executors/URLLauncher.swift new file mode 100644 index 0000000..c56e121 --- /dev/null +++ b/Sources/BubombCore/Executors/URLLauncher.swift @@ -0,0 +1,45 @@ +import Foundation +#if canImport(AppKit) +import AppKit +#endif + +/// Validates and opens URLs in the user's default web browser. +public final class URLLauncher: Sendable { + public init() {} + + public func open(urlString: String) async throws -> ExecutionResult { + let startTime = Date() + let validatedURL = try PathValidator.validateURL(urlString).get() + + var success = false + + #if canImport(AppKit) + success = await MainActor.run { + NSWorkspace.shared.open(validatedURL) + } + #endif + + if !success { + // Fallback to /usr/bin/open if NSWorkspace is not available or failed + let runner = ProcessRunner() + let result = try await runner.run( + executableURL: URL(fileURLWithPath: "/usr/bin/open"), + arguments: [validatedURL.absoluteString] + ) + return ExecutionResult( + exitCode: result.exitCode, + stdout: "Opened \(validatedURL.absoluteString) via open tool", + stderr: result.stderr, + duration: Date().timeIntervalSince(startTime) + ) + } + + let duration = Date().timeIntervalSince(startTime) + return ExecutionResult( + exitCode: 0, + stdout: "Successfully opened URL: \(validatedURL.absoluteString)", + stderr: "", + duration: duration + ) + } +} |
