mas/Sources/MasKit/Commands/Info.swift

61 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
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
public init() {
self.init(storeSearch: MasStoreSearch())
}
/// Designated initializer.
init(storeSearch: StoreSearch = MasStoreSearch()) {
self.storeSearch = storeSearch
}
/// Runs the command.
2021-03-22 05:46:17 +00:00
public func run(_ options: InfoOptions) -> Result<Void, MASError> {
do {
guard let result = try storeSearch.lookup(app: options.appId).wait() else {
2019-01-30 06:15:24 +00:00
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 {
2021-03-22 05:46:17 +00:00
InfoOptions(appId: appId)
2016-10-21 23:02:19 +00:00
}
2019-01-12 01:06:02 +00:00
public static func evaluate(_ mode: CommandMode) -> Result<InfoOptions, CommandantError<MASError>> {
2021-03-22 05:46:17 +00:00
create
<*> mode <| Argument(usage: "ID of app to show info")
2016-10-21 23:02:19 +00:00
}
}