2017-11-28 12:39:44 +00:00
|
|
|
//
|
|
|
|
// Purchase.swift
|
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Jakob Rieck on 24/10/2017.
|
|
|
|
// Copyright (c) 2017 Jakob Rieck. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2020-01-09 11:54:01 +00:00
|
|
|
import Commandant
|
|
|
|
import CommerceKit
|
2017-11-28 12:39:44 +00:00
|
|
|
|
2020-01-09 11:54:01 +00:00
|
|
|
public struct PurchaseCommand: CommandProtocol {
|
2020-05-15 03:32:51 +00:00
|
|
|
public typealias Options = PurchaseOptions
|
|
|
|
public let verb = "purchase"
|
|
|
|
public let function = "Purchase and download free apps from the Mac App Store"
|
2020-01-09 11:54:01 +00:00
|
|
|
|
2020-05-15 03:07:07 +00:00
|
|
|
private let appLibrary: AppLibrary
|
|
|
|
|
|
|
|
/// Public initializer.
|
2020-05-15 03:32:51 +00:00
|
|
|
public init() {
|
2020-05-15 03:07:07 +00:00
|
|
|
self.init(appLibrary: MasAppLibrary())
|
2020-05-15 03:32:51 +00:00
|
|
|
}
|
2020-01-09 11:54:01 +00:00
|
|
|
|
2020-05-15 03:07:07 +00:00
|
|
|
/// Internal initializer.
|
|
|
|
/// - Parameter appLibrary: AppLibrary manager.
|
|
|
|
init(appLibrary: AppLibrary = MasAppLibrary()) {
|
|
|
|
self.appLibrary = appLibrary
|
|
|
|
}
|
|
|
|
|
2020-05-15 03:32:51 +00:00
|
|
|
/// Runs the command.
|
2021-03-22 05:46:17 +00:00
|
|
|
public func run(_ options: Options) -> Result<Void, MASError> {
|
2021-11-03 20:49:32 +00:00
|
|
|
if #available(macOS 10.15, *) {
|
|
|
|
// Purchases are no longer possible as of Catalina.
|
|
|
|
// https://github.com/mas-cli/mas/issues/289
|
|
|
|
return .failure(.notSupported)
|
|
|
|
}
|
|
|
|
|
2020-05-15 03:32:51 +00:00
|
|
|
// Try to download applications with given identifiers and collect results
|
2021-05-06 22:14:56 +00:00
|
|
|
let appIds = options.appIds.filter { appId in
|
2020-05-15 03:07:07 +00:00
|
|
|
if let product = appLibrary.installedApp(forId: appId) {
|
2020-05-15 03:32:51 +00:00
|
|
|
printWarning("\(product.appName) has already been purchased.")
|
2021-05-06 22:14:56 +00:00
|
|
|
return false
|
2020-05-15 03:32:51 +00:00
|
|
|
}
|
2017-11-28 12:39:44 +00:00
|
|
|
|
2021-05-06 22:14:56 +00:00
|
|
|
return true
|
2020-05-15 03:32:51 +00:00
|
|
|
}
|
2017-11-28 12:39:44 +00:00
|
|
|
|
2021-05-06 22:14:56 +00:00
|
|
|
do {
|
|
|
|
try downloadAll(appIds, purchase: true).wait()
|
|
|
|
} catch {
|
|
|
|
return .failure(error as? MASError ?? .downloadFailed(error: error as NSError))
|
2020-05-15 03:32:51 +00:00
|
|
|
}
|
2021-05-06 22:14:56 +00:00
|
|
|
|
|
|
|
return .success(())
|
2020-05-15 03:32:51 +00:00
|
|
|
}
|
2017-11-28 12:39:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 11:54:01 +00:00
|
|
|
public struct PurchaseOptions: OptionsProtocol {
|
2020-05-15 03:32:51 +00:00
|
|
|
let appIds: [UInt64]
|
2017-11-28 12:39:44 +00:00
|
|
|
|
2020-05-15 03:32:51 +00:00
|
|
|
public static func create(_ appIds: [Int]) -> PurchaseOptions {
|
2021-03-22 05:46:17 +00:00
|
|
|
PurchaseOptions(appIds: appIds.map { UInt64($0) })
|
2020-05-15 03:32:51 +00:00
|
|
|
}
|
2017-11-28 12:39:44 +00:00
|
|
|
|
2020-05-15 03:32:51 +00:00
|
|
|
public static func evaluate(_ mode: CommandMode) -> Result<PurchaseOptions, CommandantError<MASError>> {
|
2021-03-22 05:46:17 +00:00
|
|
|
create
|
2020-05-15 03:32:51 +00:00
|
|
|
<*> mode <| Argument(usage: "app ID(s) to install")
|
|
|
|
}
|
2017-11-28 12:39:44 +00:00
|
|
|
}
|