2016-04-14 16:14:06 +00:00
|
|
|
//
|
|
|
|
// Search.swift
|
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Michael Schneider on 4/14/16.
|
|
|
|
// Copyright © 2016 Andrew Naylor. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-07-04 20:56:10 +00:00
|
|
|
import Commandant
|
|
|
|
|
2018-12-27 16:50:44 +00:00
|
|
|
/// Search the Mac App Store using the iTunes Search API:
|
|
|
|
/// https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/
|
2018-10-14 21:35:48 +00:00
|
|
|
public struct SearchCommand: CommandProtocol {
|
|
|
|
public typealias Options = SearchOptions
|
|
|
|
public let verb = "search"
|
|
|
|
public let function = "Search for apps from the Mac App Store"
|
|
|
|
|
2019-01-12 02:46:23 +00:00
|
|
|
private let storeSearch: StoreSearch
|
2018-11-14 04:47:06 +00:00
|
|
|
|
2019-01-12 02:46:23 +00:00
|
|
|
/// Designated initializer.
|
|
|
|
///
|
|
|
|
/// - Parameter storeSearch: Search manager.
|
|
|
|
public init(storeSearch: StoreSearch = MasStoreSearch()) {
|
|
|
|
self.storeSearch = storeSearch
|
2018-11-14 04:47:06 +00:00
|
|
|
}
|
2018-11-12 20:31:14 +00:00
|
|
|
|
2021-03-22 05:46:17 +00:00
|
|
|
public func run(_ options: Options) -> Result<Void, MASError> {
|
2019-01-12 02:46:23 +00:00
|
|
|
do {
|
|
|
|
let resultList = try storeSearch.search(for: options.appName)
|
|
|
|
if resultList.resultCount <= 0 || resultList.results.isEmpty {
|
|
|
|
print("No results found")
|
|
|
|
return .failure(.noSearchResultsFound)
|
2018-06-25 14:18:00 +00:00
|
|
|
}
|
2019-01-12 01:06:02 +00:00
|
|
|
|
2019-01-12 02:46:23 +00:00
|
|
|
let output = SearchResultFormatter.format(results: resultList.results, includePrice: options.price)
|
|
|
|
print(output)
|
2019-01-12 01:06:02 +00:00
|
|
|
|
2019-01-12 02:46:23 +00:00
|
|
|
return .success(())
|
|
|
|
} catch {
|
|
|
|
// Bubble up MASErrors
|
|
|
|
if let error = error as? MASError {
|
|
|
|
return .failure(error)
|
2016-04-14 16:14:06 +00:00
|
|
|
}
|
2019-01-12 02:46:23 +00:00
|
|
|
return .failure(.searchFailed)
|
2016-04-14 16:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 21:35:48 +00:00
|
|
|
public struct SearchOptions: OptionsProtocol {
|
2016-04-14 16:14:06 +00:00
|
|
|
let appName: String
|
2018-06-25 14:18:00 +00:00
|
|
|
let price: Bool
|
2018-10-14 21:35:48 +00:00
|
|
|
|
|
|
|
public static func create(_ appName: String) -> (_ price: Bool) -> SearchOptions {
|
2021-03-22 05:46:17 +00:00
|
|
|
{ price in
|
2018-06-25 14:18:00 +00:00
|
|
|
SearchOptions(appName: appName, price: price)
|
|
|
|
}
|
2016-04-14 16:14:06 +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<SearchOptions, CommandantError<MASError>> {
|
2021-03-22 05:46:17 +00:00
|
|
|
create
|
2019-01-12 01:06:02 +00:00
|
|
|
<*> mode <| Argument(usage: "the app name to search")
|
|
|
|
<*> mode <| Option(key: "price", defaultValue: false, usage: "Show price of found apps")
|
2016-04-14 16:14:06 +00:00
|
|
|
}
|
|
|
|
}
|