blob: c56e1211c14dc060212f8c7b7464463b2d4efc64 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
)
}
}
|