mas/MasKit/Commands/Install.swift

70 lines
2.1 KiB
Swift
Raw Normal View History

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