mas/MasKit/Commands/Lucky.swift

94 lines
3.2 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 urlSession: URLSession
/// Designated initializer.
///
/// - Parameter appLibrary: AppLibrary manager.
/// - Parameter urlSession: URL session for network communication.
public init(appLibrary: AppLibrary = MasAppLibrary(), urlSession: URLSession = URLSession.shared) {
self.appLibrary = appLibrary
self.urlSession = urlSession
}
public func run(_ options: Options) -> Result<(), MASError> {
2017-05-11 20:29:18 +00:00
guard let searchURLString = searchURLString(options.appName),
let searchJson = urlSession.requestSynchronousJSONWithURLString(searchURLString) as? [String: Any] else {
2017-05-11 20:29:18 +00:00
return .failure(.searchFailed)
}
guard let resultCount = searchJson[ResultKeys.ResultCount] as? Int, resultCount > 0,
let results = searchJson[ResultKeys.Results] as? [[String: Any]] else {
print("No results found")
return .failure(.noSearchResultsFound)
}
let appId = results[0][ResultKeys.TrackId] as! UInt64
2017-05-11 20:29:18 +00:00
return install(appId, options: options)
}
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))
}
}
func searchURLString(_ appName: String) -> String? {
2018-01-27 23:51:05 +00:00
if let urlEncodedAppName = appName.URLEncodedString {
2017-05-11 20:29:18 +00:00
return "https://itunes.apple.com/search?entity=macSoftware&term=\(urlEncodedAppName)&attribute=allTrackTerm"
}
return 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)
}
}
public static func evaluate(_ m: CommandMode) -> Result<LuckyOptions, CommandantError<MASError>> {
2017-05-11 20:29:18 +00:00
return create
<*> m <| Argument(usage: "the app name to install")
<*> m <| Switch(flag: nil, key: "force", usage: "force reinstall")
}
}