Skip to content

Commit

Permalink
Update Request.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
jihoonahn authored Dec 22, 2024
1 parent e7f3833 commit 9e8b325
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions Sources/Command/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ public struct Request {
/// The environment to use for the command.
public var environment: Environment? {
didSet {
self.executableURL = ""
if audited {
self.executableURL = getAbsoluteExecutableURL()
}
}
}

/// The URL of the executable to run.
public var executableURL: String {
didSet {
self.executableURL = ""
if audited {
self.executableURL = getAbsoluteExecutableURL()
}
}
}

Expand All @@ -38,3 +42,25 @@ public struct Request {
self.audited = audited
}
}

private extension Request {
func getAbsoluteExecutableURL() -> String {
if FileManager.default.fileExists(atPath: executableURL) {
return executableURL
}
guard let environment = environment else {
return executableURL
}
let paths = environment["PATH"]?.split(separator: ":").map{ String($0) } ?? []
guard paths.count > 0 else {
return executableURL
}
paths.forEach {
let absoluteExecutableURL = "\(path)/\(executableURL)"
if FileManager.default.fileExists(atPath: absoluteExecutableURL) {
return absoluteExecutableURL
}
}
return executableURL
}
}

0 comments on commit 9e8b325

Please sign in to comment.