mas/mas-cli/Commands/Upgrade.swift

71 lines
2.1 KiB
Swift
Raw Normal View History

//
// Upgrade.swift
// mas-cli
//
// Created by Andrew Naylor on 30/12/2015.
// Copyright © 2015 Andrew Naylor. All rights reserved.
//
struct UpgradeCommand: CommandType {
2016-09-14 21:55:04 +00:00
typealias Options = UpgradeOptions
let verb = "upgrade"
2016-09-14 21:55:04 +00:00
let function = "Upgrade outdated apps from the Mac App Store"
2016-09-17 12:58:38 +00:00
func run(_ options: Options) -> Result<(), MASError> {
let updateController = CKUpdateController.shared()
2015-12-30 21:20:25 +00:00
2016-09-17 12:58:38 +00:00
guard let pendingUpdates = updateController?.availableUpdates() else {
return .failure(MASError(code: .noUpdatesFound))
2016-09-14 21:55:04 +00:00
}
let updates: [CKUpdate]
2016-09-15 09:13:24 +00:00
let appIds = options.appIds
if appIds.count > 0 {
2016-09-14 21:55:04 +00:00
updates = pendingUpdates.filter {
2016-09-17 12:58:38 +00:00
appIds.contains($0.itemIdentifier.uint64Value)
2016-09-14 21:55:04 +00:00
}
guard updates.count > 0 else {
warn("Nothing found to upgrade")
2016-09-17 12:58:38 +00:00
return .success(())
}
2016-09-14 21:55:04 +00:00
} else {
// Upgrade everything
guard pendingUpdates.count > 0 else {
print("Everything is up-to-date")
2016-09-17 12:58:38 +00:00
return .success(())
2016-09-14 21:55:04 +00:00
}
updates = pendingUpdates
2015-12-30 21:20:25 +00:00
}
print("Upgrading \(updates.count) outdated application\(updates.count > 1 ? "s" : ""):")
2016-09-17 12:58:38 +00:00
print(updates.map({ "\($0.title) (\($0.bundleVersion))" }).joined(separator: ", "))
2016-09-14 21:55:04 +00:00
let updateResults = updates.flatMap {
2016-09-17 12:58:38 +00:00
download($0.itemIdentifier.uint64Value)
2016-09-14 21:55:04 +00:00
}
switch updateResults.count {
case 0:
2016-09-17 12:58:38 +00:00
return .success()
2016-09-14 21:55:04 +00:00
case 1:
2016-09-17 12:58:38 +00:00
return .failure(updateResults[0])
2016-09-14 21:55:04 +00:00
default:
2016-09-17 12:58:38 +00:00
return .failure(MASError(code: .downloadFailed))
}
}
2015-12-30 21:20:25 +00:00
}
2016-09-14 21:55:04 +00:00
struct UpgradeOptions: OptionsType {
2016-09-15 09:13:24 +00:00
let appIds: [UInt64]
2016-09-14 21:55:04 +00:00
2016-09-17 12:58:38 +00:00
static func create(_ appIds: [Int]) -> UpgradeOptions {
2016-09-14 21:55:04 +00:00
return UpgradeOptions(appIds: appIds.map { UInt64($0) })
}
2016-09-17 12:58:38 +00:00
static func evaluate(_ m: CommandMode) -> Result<UpgradeOptions, CommandantError<MASError>> {
2016-09-14 21:55:04 +00:00
return create
2016-09-15 09:13:24 +00:00
<*> m <| Argument(defaultValue: [], usage: "app ID(s) to install")
2016-09-14 21:55:04 +00:00
}
2016-09-17 12:58:38 +00:00
}