1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2026-03-27 22:40:08 +01:00

🐛 Tweak handling of RealShell

This commit avoids the usage of `readDataToEndOfFile` and instead uses
a helper method that:

- Attempts to use `.readToEnd()`
- Has a fallback to return an empty string
- Attempts to close the pipe
This commit is contained in:
2025-11-27 18:47:39 +01:00
parent 89a83bcb4e
commit 464fa78deb

View File

@@ -80,6 +80,23 @@ class RealShell: ShellProtocol {
return task
}
/**
Reads the entire output of a `Pipe` and returns it as a UTF8 string.
Closes the pipe's file handler when done.
*/
private static func getStringOutput(from pipe: Pipe) -> String {
// 1. Read all data (safely).
let rawData = (try? pipe.fileHandleForReading.readToEnd()) ?? Data()
// 2. Convert to string (safely).
let result = String(data: rawData, encoding: .utf8) ?? ""
// 3. Close the handle quietly.
try? pipe.fileHandleForReading.close()
return result
}
// MARK: - Public API
/**
@@ -114,11 +131,8 @@ class RealShell: ShellProtocol {
return .out("", "")
}
let stdOut = String(data: outputPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
let stdErr = String(data: errorPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
try? outputPipe.fileHandleForReading.close()
try? errorPipe.fileHandleForReading.close()
let stdOut = RealShell.getStringOutput(from: outputPipe)
let stdErr = RealShell.getStringOutput(from: errorPipe)
if Log.shared.verbosity == .cli {
log(process: process, stdOut: stdOut, stdErr: stdErr)
@@ -145,14 +159,11 @@ class RealShell: ShellProtocol {
process.terminationHandler = { [weak self] _ in
if process.terminationReason == .uncaughtSignal {
Log.err("The command `\(command)` likely crashed. Returning empty output.")
continuation.resume(returning: .out("", ""))
return continuation.resume(returning: .out("", ""))
}
let stdOut = String(data: outputPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
let stdErr = String(data: errorPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
try? outputPipe.fileHandleForReading.close()
try? errorPipe.fileHandleForReading.close()
let stdOut = RealShell.getStringOutput(from: outputPipe)
let stdErr = RealShell.getStringOutput(from: errorPipe)
if Log.shared.verbosity == .cli {
self?.log(process: process, stdOut: stdOut, stdErr: stdErr)