Modernize quarantine.swift

This commit is contained in:
Dabezt 2021-12-31 13:23:45 +03:00
parent 0b14b6d76d
commit e4f106533e

View File

@ -2,46 +2,53 @@
import Foundation import Foundation
struct swifterr: TextOutputStream { struct SwiftErr: TextOutputStream {
public static var stream = swifterr() public static var stream = SwiftErr()
mutating func write(_ string: String) { fputs(string, stderr) }
mutating func write(_ string: String) {
fputs(string, stderr)
}
} }
if #available(macOS 10.10, *) { // Make sure the user is on macOS 10.10 or newer
if (CommandLine.arguments.count < 4) { guard #available(macOS 10.10, *) else {
print("Homebrew Quarantine: user must be on macOS 10.10 or newer.", to: &SwiftErr.stream)
exit(5)
}
// TODO: tell which arguments have to be provided
guard CommandLine.arguments.count >= 4 else {
exit(2) exit(2)
} }
let dataLocationUrl: NSURL = NSURL.init(fileURLWithPath: CommandLine.arguments[1]) var dataLocationURL = URL(fileURLWithPath: CommandLine.arguments[1])
var errorBag: NSError? let quarantineProperties: [String: Any] = [
let quarantineProperties: [String: Any] = [
kLSQuarantineAgentNameKey as String: "Homebrew Cask", kLSQuarantineAgentNameKey as String: "Homebrew Cask",
kLSQuarantineTypeKey as String: kLSQuarantineTypeWebDownload, kLSQuarantineTypeKey as String: kLSQuarantineTypeWebDownload,
kLSQuarantineDataURLKey as String: CommandLine.arguments[2], kLSQuarantineDataURLKey as String: CommandLine.arguments[2],
kLSQuarantineOriginURLKey as String: CommandLine.arguments[3] kLSQuarantineOriginURLKey as String: CommandLine.arguments[3]
] ]
if (dataLocationUrl.checkResourceIsReachableAndReturnError(&errorBag)) { // Check for if the data location URL is reachable
do { do {
try dataLocationUrl.setResourceValue( let isDataLocationURLReachable = try dataLocationURL.checkResourceIsReachable()
quarantineProperties as NSDictionary, guard isDataLocationURLReachable else {
forKey: URLResourceKey.quarantinePropertiesKey print("URL \(dataLocationURL.path) is not reachable. Not proceeding.", to: &SwiftErr.stream)
) exit(1)
} }
catch { } catch {
print(error.localizedDescription, to: &swifterr.stream) print(error.localizedDescription, to: &SwiftErr.stream)
exit(1) exit(1)
} }
}
else {
print(errorBag!.localizedDescription, to: &swifterr.stream)
exit(3)
}
exit(0) // Quarantine the file
} do {
else { var resourceValues = URLResourceValues()
exit(5) resourceValues.quarantineProperties = quarantineProperties
try dataLocationURL.setResourceValues(resourceValues)
} catch {
print(error.localizedDescription, to: &SwiftErr.stream)
exit(1)
} }