mas/mas-cli/Commands/Install.swift

57 lines
1.8 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.
//
struct InstallCommand: CommandType {
2015-12-30 21:20:25 +00:00
typealias Options = InstallOptions
let verb = "install"
let function = "Install from the Mac App Store"
2015-12-30 21:20:25 +00:00
func run(options: Options) -> Result<(), MASError> {
// Try to download applications with given identifiers and collect results
let downloadResults = options.appIds.flatMap { (appId) -> MASError? in
2016-09-14 22:04:53 +00:00
if let product = installedApp(appId) where !options.forceInstall {
warn("\(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-14 21:07:56 +00:00
return .Failure(downloadResults[0])
default:
return .Failure(MASError(code: .DownloadFailed))
}
}
private func installedApp(appId: UInt64) -> CKSoftwareProduct? {
let appId = NSNumber(unsignedLongLong: appId)
2016-09-14 21:07:56 +00:00
let softwareMap = CKSoftwareMap.sharedSoftwareMap()
return softwareMap.allProducts()?.filter { $0.itemIdentifier == appId }.first
}
}
struct InstallOptions: OptionsType {
let appIds: [UInt64]
2016-09-14 22:04:53 +00:00
let forceInstall: Bool
2016-09-14 22:04:53 +00:00
static func create(appIds: [Int], forceInstall: Bool) -> InstallOptions {
return InstallOptions(appIds: appIds.map{UInt64($0)}, forceInstall: forceInstall)
}
static func evaluate(m: CommandMode) -> Result<InstallOptions, CommandantError<MASError>> {
2016-09-14 22:04:53 +00:00
return curry(InstallOptions.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")
}
}