2015-08-21 13:02:36 +00:00
|
|
|
//
|
|
|
|
// Install.swift
|
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Andrew Naylor on 21/08/2015.
|
|
|
|
// Copyright (c) 2015 Andrew Naylor. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-07-04 20:56:10 +00:00
|
|
|
import Commandant
|
|
|
|
import Result
|
2018-10-14 19:41:19 +00:00
|
|
|
import CommerceKit
|
2018-07-04 20:56:10 +00:00
|
|
|
|
2018-12-28 22:08:31 +00:00
|
|
|
/// Installs previously purchased apps from the Mac App Store.
|
2018-10-14 21:35:48 +00:00
|
|
|
public struct InstallCommand: CommandProtocol {
|
|
|
|
public typealias Options = InstallOptions
|
|
|
|
public let verb = "install"
|
|
|
|
public let function = "Install from the Mac App Store"
|
|
|
|
|
2018-12-28 22:08:31 +00:00
|
|
|
private let appLibrary: AppLibrary
|
|
|
|
|
|
|
|
/// Designated initializer.
|
|
|
|
///
|
|
|
|
/// - Parameter appLibrary: AppLibrary manager.
|
|
|
|
public init(appLibrary: AppLibrary = MasAppLibrary()) {
|
|
|
|
self.appLibrary = appLibrary
|
|
|
|
}
|
2018-12-27 21:02:14 +00:00
|
|
|
|
2019-01-12 01:28:03 +00:00
|
|
|
/// Runs the command.
|
2018-10-14 21:35:48 +00:00
|
|
|
public func run(_ options: Options) -> Result<(), MASError> {
|
2016-09-06 14:28:24 +00:00
|
|
|
// Try to download applications with given identifiers and collect results
|
2018-04-04 01:34:45 +00:00
|
|
|
let downloadResults = options.appIds.compactMap { (appId) -> MASError? in
|
2018-12-28 22:08:31 +00:00
|
|
|
if let product = appLibrary.installedApp(forId: appId), !options.forceInstall {
|
2016-09-25 21:13:23 +00:00
|
|
|
printWarning("\(product.appName) is already installed")
|
2016-09-14 21:28:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-12-27 21:02:14 +00:00
|
|
|
|
2016-09-14 21:28:18 +00:00
|
|
|
return download(appId)
|
|
|
|
}
|
2016-09-14 21:07:56 +00:00
|
|
|
|
2016-09-14 21:28:18 +00:00
|
|
|
switch downloadResults.count {
|
|
|
|
case 0:
|
2017-12-28 15:45:08 +00:00
|
|
|
return .success(())
|
2016-09-14 21:28:18 +00:00
|
|
|
case 1:
|
2016-09-17 12:58:38 +00:00
|
|
|
return .failure(downloadResults[0])
|
2016-09-14 21:28:18 +00:00
|
|
|
default:
|
2016-09-25 21:13:23 +00:00
|
|
|
return .failure(.downloadFailed(error: nil))
|
2016-09-06 14:28:24 +00:00
|
|
|
}
|
2016-09-14 21:28:18 +00:00
|
|
|
}
|
2015-08-21 13:02:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-14 21:35:48 +00:00
|
|
|
public struct InstallOptions: OptionsProtocol {
|
2016-09-06 13:37:12 +00:00
|
|
|
let appIds: [UInt64]
|
2016-09-14 22:04:53 +00:00
|
|
|
let forceInstall: Bool
|
2018-10-14 21:35:48 +00:00
|
|
|
|
|
|
|
public static func create(_ appIds: [Int]) -> (_ forceInstall: Bool) -> InstallOptions {
|
2016-09-17 12:58:38 +00:00
|
|
|
return { forceInstall in
|
2019-01-12 01:06:02 +00:00
|
|
|
return InstallOptions(appIds: appIds.map {UInt64($0)}, forceInstall: forceInstall)
|
2016-09-17 12:58:38 +00:00
|
|
|
}
|
2015-08-21 13:02:36 +00:00
|
|
|
}
|
2018-10-14 21:35:48 +00:00
|
|
|
|
2019-01-12 01:06:02 +00:00
|
|
|
public static func evaluate(_ mode: CommandMode) -> Result<InstallOptions, CommandantError<MASError>> {
|
2016-09-17 12:58:38 +00:00
|
|
|
return create
|
2019-01-12 01:06:02 +00:00
|
|
|
<*> mode <| Argument(usage: "app ID(s) to install")
|
|
|
|
<*> mode <| Switch(flag: nil, key: "force", usage: "force reinstall")
|
2015-08-21 13:02:36 +00:00
|
|
|
}
|
2016-09-14 21:28:18 +00:00
|
|
|
}
|