2016-10-21 23:02:19 +00:00
|
|
|
//
|
|
|
|
// Info.swift
|
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Denis Lebedev on 21/10/2016.
|
|
|
|
// Copyright © 2016 Andrew Naylor. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-07-04 20:56:10 +00:00
|
|
|
import Commandant
|
2016-10-21 23:02:19 +00:00
|
|
|
import Foundation
|
|
|
|
|
2018-12-30 06:57:06 +00:00
|
|
|
/// Displays app details. Uses the iTunes Lookup API:
|
|
|
|
/// https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/#lookup
|
2018-10-14 21:35:48 +00:00
|
|
|
public struct InfoCommand: CommandProtocol {
|
|
|
|
public let verb = "info"
|
|
|
|
public let function = "Display app information from the Mac App Store"
|
2016-10-21 23:02:19 +00:00
|
|
|
|
2019-01-08 00:36:14 +00:00
|
|
|
private let storeSearch: StoreSearch
|
|
|
|
|
|
|
|
/// Designated initializer.
|
|
|
|
public init(storeSearch: StoreSearch = MasStoreSearch()) {
|
|
|
|
self.storeSearch = storeSearch
|
2018-11-14 04:47:06 +00:00
|
|
|
}
|
2018-10-14 21:35:48 +00:00
|
|
|
|
2019-01-08 00:36:14 +00:00
|
|
|
/// Runs the command.
|
2018-10-14 21:35:48 +00:00
|
|
|
public func run(_ options: InfoOptions) -> Result<(), MASError> {
|
2019-01-08 00:36:14 +00:00
|
|
|
do {
|
|
|
|
guard let result = try storeSearch.lookup(app: options.appId)
|
2019-01-30 06:15:24 +00:00
|
|
|
else {
|
|
|
|
print("No results found")
|
|
|
|
return .failure(.noSearchResultsFound)
|
2019-01-08 00:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
print(AppInfoFormatter.format(app: result))
|
2019-01-12 01:06:02 +00:00
|
|
|
} catch {
|
2019-01-08 00:36:14 +00:00
|
|
|
// Bubble up MASErrors
|
|
|
|
if let error = error as? MASError {
|
|
|
|
return .failure(error)
|
|
|
|
}
|
|
|
|
return .failure(.searchFailed)
|
2016-10-21 23:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return .success(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 21:35:48 +00:00
|
|
|
public struct InfoOptions: OptionsProtocol {
|
2019-01-18 05:06:25 +00:00
|
|
|
let appId: Int
|
2016-10-21 23:02:19 +00:00
|
|
|
|
2019-01-18 05:06:25 +00:00
|
|
|
static func create(_ appId: Int) -> InfoOptions {
|
2016-10-21 23:02:19 +00:00
|
|
|
return InfoOptions(appId: appId)
|
|
|
|
}
|
|
|
|
|
2019-01-12 01:06:02 +00:00
|
|
|
public static func evaluate(_ mode: CommandMode) -> Result<InfoOptions, CommandantError<MASError>> {
|
2016-10-21 23:02:19 +00:00
|
|
|
return create
|
2019-01-18 05:06:25 +00:00
|
|
|
<*> mode <| Argument(usage: "ID of app to show info")
|
2016-10-21 23:02:19 +00:00
|
|
|
}
|
|
|
|
}
|