7 ms·
tldr; Before: let process = Process() process.executableURL = URL(fileURLWithPath: "/bin/ls") let pipe = Pipe() process.standardOutput = pipe
by hooch 11mo ago
tldr;
Before:
let process = Process()
process.executableURL = URL(fileURLWithPath: "/bin/ls")
let pipe = Pipe()
process.standardOutput = pipe
try! process.run()
process.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = String(data: data, encoding: .utf8) {
print(output)
}
After:
let result = try await run(
.name("ls"),
arguments: ["-1"],
output: .string(limit: 1 << 20)
)
print(result.standardOutput ?? "")
- fainpul 11mo agoWhy bother with invoking shell programs instead of using built-in ways of the language to do the same task? let result = try! FileManager.default.contentsOfDirectory(atPath: "/etc") print(result) Various other FileManager methods are available: https://developer.apple.com/documentation/foundation/filemanager#Discovering-directory-contents https://developer.apple.com/documentation/foundation/fileman...