Make trash.swift safer and more modern
This commit is contained in:
parent
e4f106533e
commit
0b44dd5a48
@ -4,28 +4,32 @@ import Foundation
|
||||
|
||||
extension FileHandle : TextOutputStream {
|
||||
public func write(_ string: String) {
|
||||
self.write(string.data(using: .utf8)!)
|
||||
if let data = string.data(using: .utf8) { self.write(data) }
|
||||
}
|
||||
}
|
||||
|
||||
var stderr = FileHandle.standardError
|
||||
|
||||
let manager: FileManager = FileManager()
|
||||
let manager = FileManager.default
|
||||
|
||||
var success = true
|
||||
|
||||
for item in CommandLine.arguments[1...] {
|
||||
do {
|
||||
let path: URL = URL(fileURLWithPath: item)
|
||||
var trashedPath: NSURL!
|
||||
try manager.trashItem(at: path, resultingItemURL: &trashedPath)
|
||||
print((trashedPath as URL).path, terminator: ":")
|
||||
} catch {
|
||||
print(item, terminator: ":", to: &stderr)
|
||||
success = false
|
||||
}
|
||||
// The command line arguments given but without the script's name
|
||||
let CMDLineArgs = Array(CommandLine.arguments.dropFirst())
|
||||
|
||||
for item in CMDLineArgs {
|
||||
do {
|
||||
let url = URL(fileURLWithPath: item)
|
||||
var trashedPath: NSURL!
|
||||
try manager.trashItem(at: url, resultingItemURL: &trashedPath)
|
||||
print((trashedPath as URL).path, terminator: ":")
|
||||
success = true
|
||||
} catch {
|
||||
print(item, terminator: ":", to: &stderr)
|
||||
success = false
|
||||
}
|
||||
}
|
||||
|
||||
guard success else {
|
||||
exit(1)
|
||||
exit(1)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user