mas/MasKit/Commands/Search.swift

65 lines
2 KiB
Swift
Raw Normal View History

//
// 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/
public struct SearchCommand: CommandProtocol {
public typealias Options = SearchOptions
public let verb = "search"
public let function = "Search for apps from the Mac App Store"
private let storeSearch: StoreSearch
/// Designated initializer.
///
/// - Parameter storeSearch: Search manager.
public init(storeSearch: StoreSearch = MasStoreSearch()) {
self.storeSearch = storeSearch
}
2018-11-12 20:31:14 +00:00
2021-03-22 05:46:17 +00:00
public func run(_ options: Options) -> Result<Void, MASError> {
do {
let resultList = try storeSearch.search(for: options.appName)
if resultList.resultCount <= 0 || resultList.results.isEmpty {
print("No results found")
return .failure(.noSearchResultsFound)
}
2019-01-12 01:06:02 +00:00
let output = SearchResultFormatter.format(results: resultList.results, includePrice: options.price)
print(output)
2019-01-12 01:06:02 +00:00
return .success(())
} catch {
// Bubble up MASErrors
if let error = error as? MASError {
return .failure(error)
}
return .failure(.searchFailed)
}
}
}
public struct SearchOptions: OptionsProtocol {
let appName: String
let price: Bool
public static func create(_ appName: String) -> (_ price: Bool) -> SearchOptions {
2021-03-22 05:46:17 +00:00
{ price in
SearchOptions(appName: appName, price: price)
}
}
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")
}
}