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
|
2018-10-14 19:41:19 +00:00
|
|
|
import CommerceKit
|
2019-01-30 06:15:24 +00:00
|
|
|
import Result
|
2018-10-14 19:41:19 +00:00
|
|
|
|
2018-12-28 22:08:31 +00:00
|
|
|
/// Command which installs the first search result. This is handy as many MAS titles
|
|
|
|
/// can be long with embedded keywords.
|
2018-10-14 21:35:48 +00:00
|
|
|
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
|
|
|
|
2018-12-28 22:08:31 +00:00
|
|
|
private let appLibrary: AppLibrary
|
2019-01-12 00:10:36 +00:00
|
|
|
private let storeSearch: StoreSearch
|
2018-11-14 04:47:06 +00:00
|
|
|
|
2018-12-28 22:08:31 +00:00
|
|
|
/// Designated initializer.
|
|
|
|
///
|
|
|
|
/// - Parameter appLibrary: AppLibrary manager.
|
2019-01-12 00:10:36 +00:00
|
|
|
/// - Parameter storeSearch: Search manager.
|
|
|
|
public init(appLibrary: AppLibrary = MasAppLibrary(),
|
|
|
|
storeSearch: StoreSearch = MasStoreSearch()) {
|
2018-12-28 22:08:31 +00:00
|
|
|
self.appLibrary = appLibrary
|
2019-01-12 00:10:36 +00:00
|
|
|
self.storeSearch = storeSearch
|
2018-11-14 04:47:06 +00:00
|
|
|
}
|
2018-10-14 21:35:48 +00:00
|
|
|
|
2019-01-12 00:10:36 +00:00
|
|
|
/// Runs the command.
|
2018-10-14 21:35:48 +00:00
|
|
|
public func run(_ options: Options) -> Result<(), MASError> {
|
2019-01-12 00:10:36 +00:00
|
|
|
var appId: Int?
|
|
|
|
|
|
|
|
do {
|
2019-01-18 05:05:16 +00:00
|
|
|
let results = try storeSearch.search(for: options.appName)
|
|
|
|
guard let result = results.results.first else {
|
|
|
|
print("No results found")
|
|
|
|
return .failure(.noSearchResultsFound)
|
2019-01-12 00:10:36 +00:00
|
|
|
}
|
2017-05-11 20:29:18 +00:00
|
|
|
|
2019-01-12 00:10:36 +00:00
|
|
|
appId = result.trackId
|
2019-01-12 01:06:02 +00:00
|
|
|
} catch {
|
2019-01-12 00:10:36 +00:00
|
|
|
// 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() }
|
2018-12-28 22:08:31 +00:00
|
|
|
|
2019-01-12 01:06:02 +00:00
|
|
|
return install(UInt64(identifier), options: options)
|
2017-05-11 20:29:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 00:10:36 +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
|
2018-04-04 01:34:45 +00:00
|
|
|
let downloadResults = [appId].compactMap { (appId) -> MASError? in
|
2018-12-28 22:08:31 +00:00
|
|
|
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
|
|
|
|
}
|
2018-12-28 22:08:31 +00:00
|
|
|
|
2017-05-11 20:29:18 +00:00
|
|
|
return download(appId)
|
|
|
|
}
|
2018-12-28 22:08:31 +00:00
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 21:35:48 +00:00
|
|
|
public struct LuckyOptions: OptionsProtocol {
|
2017-05-11 20:29:18 +00:00
|
|
|
let appName: String
|
|
|
|
let forceInstall: Bool
|
2018-10-14 21:35:48 +00:00
|
|
|
|
|
|
|
public static func create(_ appName: String) -> (_ forceInstall: Bool) -> LuckyOptions {
|
2017-05-11 20:29:18 +00:00
|
|
|
return { forceInstall in
|
2019-01-30 06:15:24 +00:00
|
|
|
LuckyOptions(appName: appName, forceInstall: forceInstall)
|
2017-05-11 20:29:18 +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<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
|
|
|
}
|
|
|
|
}
|