mas/MasKit/Commands/Info.swift

60 lines
1.6 KiB
Swift
Raw Normal View History

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
2019-01-30 06:15:24 +00:00
import Result
2016-10-21 23:02:19 +00:00
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
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
private let storeSearch: StoreSearch
/// Designated initializer.
public init(storeSearch: StoreSearch = MasStoreSearch()) {
self.storeSearch = storeSearch
}
/// Runs the command.
public func run(_ options: InfoOptions) -> Result<(), MASError> {
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)
}
print(AppInfoFormatter.format(app: result))
2019-01-12 01:06:02 +00:00
} catch {
// 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(())
}
}
public struct InfoOptions: OptionsProtocol {
let appId: Int
2016-10-21 23:02:19 +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
<*> mode <| Argument(usage: "ID of app to show info")
2016-10-21 23:02:19 +00:00
}
}