Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize swift scripts in Library/Homebrew/cask/utils #12651

Merged
merged 2 commits into from Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 38 additions & 31 deletions Library/Homebrew/cask/utils/quarantine.swift
Expand Up @@ -2,46 +2,53 @@

import Foundation

struct swifterr: TextOutputStream {
public static var stream = swifterr()
mutating func write(_ string: String) { fputs(string, stderr) }
struct SwiftErr: TextOutputStream {
public static var stream = SwiftErr()

mutating func write(_ string: String) {
fputs(string, stderr)
}
}

if #available(macOS 10.10, *) {
if (CommandLine.arguments.count < 4) {
exit(2)
}
// Make sure the user is on macOS 10.10 or newer
guard #available(macOS 10.10, *) else {
print("Homebrew Quarantine: user must be on macOS 10.10 or newer.", to: &SwiftErr.stream)
exit(5)
}

let dataLocationUrl: NSURL = NSURL.init(fileURLWithPath: CommandLine.arguments[1])

var errorBag: NSError?
// TODO: tell which arguments have to be provided
guard CommandLine.arguments.count >= 4 else {
exit(2)
}

let quarantineProperties: [String: Any] = [
var dataLocationURL = URL(fileURLWithPath: CommandLine.arguments[1])

let quarantineProperties: [String: Any] = [
kLSQuarantineAgentNameKey as String: "Homebrew Cask",
kLSQuarantineTypeKey as String: kLSQuarantineTypeWebDownload,
kLSQuarantineDataURLKey as String: CommandLine.arguments[2],
kLSQuarantineOriginURLKey as String: CommandLine.arguments[3]
]

if (dataLocationUrl.checkResourceIsReachableAndReturnError(&errorBag)) {
do {
try dataLocationUrl.setResourceValue(
quarantineProperties as NSDictionary,
forKey: URLResourceKey.quarantinePropertiesKey
)
}
catch {
print(error.localizedDescription, to: &swifterr.stream)
exit(1)
]

// Check for if the data location URL is reachable
do {
let isDataLocationURLReachable = try dataLocationURL.checkResourceIsReachable()
guard isDataLocationURLReachable else {
print("URL \(dataLocationURL.path) is not reachable. Not proceeding.", to: &SwiftErr.stream)
exit(1)
}
}
else {
print(errorBag!.localizedDescription, to: &swifterr.stream)
exit(3)
}

exit(0)
} catch {
print(error.localizedDescription, to: &SwiftErr.stream)
exit(1)
}
else {
exit(5)

// Quarantine the file
do {
var resourceValues = URLResourceValues()
resourceValues.quarantineProperties = quarantineProperties
try dataLocationURL.setResourceValues(resourceValues)
} catch {
print(error.localizedDescription, to: &SwiftErr.stream)
exit(1)
}
30 changes: 17 additions & 13 deletions Library/Homebrew/cask/utils/trash.swift
Expand Up @@ -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)
}