Mac: privileged CLI helper install via osascript
This commit is contained in:
@@ -30,19 +30,48 @@ enum CLIInstaller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let targets = ["/usr/local/bin/clawdis-mac", "/opt/homebrew/bin/clawdis-mac"]
|
let targets = ["/usr/local/bin/clawdis-mac", "/opt/homebrew/bin/clawdis-mac"]
|
||||||
var messages: [String] = []
|
let result = await self.privilegedSymlink(source: helper.path, targets: targets)
|
||||||
for target in targets {
|
await statusHandler(result)
|
||||||
do {
|
}
|
||||||
try FileManager.default.createDirectory(
|
|
||||||
atPath: (target as NSString).deletingLastPathComponent,
|
private static func privilegedSymlink(source: String, targets: [String]) async -> String {
|
||||||
withIntermediateDirectories: true)
|
let escapedSource = self.shellEscape(source)
|
||||||
try? FileManager.default.removeItem(atPath: target)
|
let targetList = targets.map(self.shellEscape).joined(separator: " ")
|
||||||
try FileManager.default.createSymbolicLink(atPath: target, withDestinationPath: helper.path)
|
let cmds = [
|
||||||
messages.append("Linked \(target)")
|
"mkdir -p /usr/local/bin /opt/homebrew/bin",
|
||||||
} catch {
|
targets.map { "ln -sf \(escapedSource) \($0)" }.joined(separator: "; ")
|
||||||
messages.append("Failed \(target): \(error.localizedDescription)")
|
].joined(separator: "; ")
|
||||||
|
|
||||||
|
let script = """
|
||||||
|
do shell script "\(cmds)" with administrator privileges
|
||||||
|
"""
|
||||||
|
|
||||||
|
let proc = Process()
|
||||||
|
proc.executableURL = URL(fileURLWithPath: "/usr/bin/osascript")
|
||||||
|
proc.arguments = ["-e", script]
|
||||||
|
|
||||||
|
let pipe = Pipe()
|
||||||
|
proc.standardOutput = pipe
|
||||||
|
proc.standardError = pipe
|
||||||
|
|
||||||
|
do {
|
||||||
|
try proc.run()
|
||||||
|
proc.waitUntilExit()
|
||||||
|
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
||||||
|
let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
||||||
|
if proc.terminationStatus == 0 {
|
||||||
|
return output.isEmpty ? "CLI helper linked into \(targetList)" : output
|
||||||
}
|
}
|
||||||
|
if output.lowercased().contains("user canceled") {
|
||||||
|
return "Install canceled"
|
||||||
|
}
|
||||||
|
return "Failed to install CLI helper: \(output)"
|
||||||
|
} catch {
|
||||||
|
return "Failed to run installer: \(error.localizedDescription)"
|
||||||
}
|
}
|
||||||
await statusHandler(messages.joined(separator: "; "))
|
}
|
||||||
|
|
||||||
|
private static func shellEscape(_ path: String) -> String {
|
||||||
|
"'" + path.replacingOccurrences(of: "'", with: "'\"'\"'") + "'"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user