mas/MasKit/Commands/Lucky.swift

102 lines
3.1 KiB
Swift
Raw Normal View History

2017-05-11 20:29:18 +00:00
//
// Lucky.swift
// mas-cli
//
// Created by Pablo Varela on 05/11/17.
// Copyright © 2016 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
/// Command which installs the first search result. This is handy as many MAS titles
/// can be long with embedded keywords.
public struct LuckyCommand: CommandProtocol {
public typealias Options = LuckyOptions
public let verb = "lucky"
public let function = "Install the first result from the Mac App Store"
2017-05-11 20:29:18 +00:00
private let appLibrary: AppLibrary
private let storeSearch: StoreSearch
/// Designated initializer.
///
/// - Parameter appLibrary: AppLibrary manager.
/// - Parameter storeSearch: Search manager.
public init(appLibrary: AppLibrary = MasAppLibrary(),
storeSearch: StoreSearch = MasStoreSearch()) {
self.appLibrary = appLibrary
self.storeSearch = storeSearch
}
/// Runs the command.
public func run(_ options: Options) -> Result<(), MASError> {
var appId: Int?
do {
guard let result = try storeSearch.lookup(app: options.appName)
else {
print("No results found")
return .failure(.noSearchResultsFound)
}
2017-05-11 20:29:18 +00:00
appId = result.trackId
2019-01-12 01:06:02 +00:00
} catch {
// Bubble up MASErrors
if let error = error as? MASError {
return .failure(error)
}
return .failure(.searchFailed)
2017-05-11 20:29:18 +00:00
}
2019-01-12 01:06:02 +00:00
guard let identifier = appId else { fatalError() }
2019-01-12 01:06:02 +00:00
return install(UInt64(identifier), options: options)
2017-05-11 20:29:18 +00:00
}
/// Installs an app.
///
/// - Parameters:
/// - appId: App identifier
/// - options: command opetions.
/// - Returns: Result of the operation.
2017-05-11 20:29:18 +00:00
fileprivate func install(_ appId: UInt64, options: Options) -> Result<(), MASError> {
// Try to download applications with given identifiers and collect results
let downloadResults = [appId].compactMap { (appId) -> MASError? in
if let product = appLibrary.installedApp(forId: appId), !options.forceInstall {
2017-05-11 20:29:18 +00:00
printWarning("\(product.appName) is already installed")
return nil
}
2017-05-11 20:29:18 +00:00
return download(appId)
}
2017-05-11 20:29:18 +00:00
switch downloadResults.count {
case 0:
2018-01-27 23:51:05 +00:00
return .success(())
2017-05-11 20:29:18 +00:00
case 1:
return .failure(downloadResults[0])
default:
return .failure(.downloadFailed(error: nil))
}
}
}
public struct LuckyOptions: OptionsProtocol {
2017-05-11 20:29:18 +00:00
let appName: String
let forceInstall: Bool
public static func create(_ appName: String) -> (_ forceInstall: Bool) -> LuckyOptions {
2017-05-11 20:29:18 +00:00
return { forceInstall in
return LuckyOptions(appName: appName, forceInstall: forceInstall)
}
}
2019-01-12 01:06:02 +00:00
public static func evaluate(_ mode: CommandMode) -> Result<LuckyOptions, CommandantError<MASError>> {
2017-05-11 20:29:18 +00:00
return create
2019-01-12 01:06:02 +00:00
<*> mode <| Argument(usage: "the app name to install")
<*> mode <| Switch(flag: nil, key: "force", usage: "force reinstall")
2017-05-11 20:29:18 +00:00
}
}