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