mas/App/Commands/Install.swift

65 lines
2 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 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
public struct InstallCommand: CommandProtocol {
public typealias Options = InstallOptions
public let verb = "install"
public let function = "Install from the Mac App Store"
public init() {}
public func run(_ options: Options) -> Result<(), MASError> {
// Try to download applications with given identifiers and collect results
let downloadResults = options.appIds.compactMap { (appId) -> MASError? in
2016-09-17 12:58:38 +00:00
if let product = installedApp(appId) , !options.forceInstall {
2016-09-25 21:13:23 +00:00
printWarning("\(product.appName) is already installed")
return nil
}
return download(appId)
}
2016-09-14 21:07:56 +00:00
switch downloadResults.count {
case 0:
return .success(())
case 1:
2016-09-17 12:58:38 +00:00
return .failure(downloadResults[0])
default:
2016-09-25 21:13:23 +00:00
return .failure(.downloadFailed(error: nil))
}
}
2016-09-17 12:58:38 +00:00
fileprivate func installedApp(_ appId: UInt64) -> CKSoftwareProduct? {
let appId = NSNumber(value: appId)
2016-09-14 21:07:56 +00:00
2016-09-17 12:58:38 +00:00
let softwareMap = CKSoftwareMap.shared()
2016-10-21 21:59:33 +00:00
return softwareMap.allProducts()?.first { $0.itemIdentifier == appId }
}
}
public struct InstallOptions: OptionsProtocol {
let appIds: [UInt64]
2016-09-14 22:04:53 +00:00
let forceInstall: Bool
public static func create(_ appIds: [Int]) -> (_ forceInstall: Bool) -> InstallOptions {
2016-09-17 12:58:38 +00:00
return { forceInstall in
return InstallOptions(appIds: appIds.map{UInt64($0)}, forceInstall: forceInstall)
}
}
public static func evaluate(_ m: CommandMode) -> Result<InstallOptions, CommandantError<MASError>> {
2016-09-17 12:58:38 +00:00
return create
2016-09-14 21:07:56 +00:00
<*> m <| Argument(usage: "app ID(s) to install")
2016-09-14 22:04:53 +00:00
<*> m <| Switch(flag: nil, key: "force", usage: "force reinstall")
}
}