2019-01-04 01:19:44 +00:00
|
|
|
//
|
|
|
|
// Vendor.swift
|
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Ben Chatelain on 2018-12-29.
|
|
|
|
// Copyright © 2016 mas-cli. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Commandant
|
|
|
|
import Result
|
|
|
|
|
|
|
|
/// Opens vendor's app page in a browser. Uses the iTunes Lookup API:
|
|
|
|
/// https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/#lookup
|
|
|
|
public struct VendorCommand: CommandProtocol {
|
|
|
|
public typealias Options = VendorOptions
|
|
|
|
|
|
|
|
public let verb = "vendor"
|
|
|
|
public let function = "Opens vendor's app page in a browser"
|
|
|
|
|
|
|
|
private let storeSearch: StoreSearch
|
|
|
|
private var openCommand: ExternalCommand
|
|
|
|
|
|
|
|
/// Designated initializer.
|
|
|
|
public init(storeSearch: StoreSearch = MasStoreSearch(),
|
|
|
|
openCommand: ExternalCommand = OpenSystemCommand()) {
|
|
|
|
self.storeSearch = storeSearch
|
|
|
|
self.openCommand = openCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs the command.
|
|
|
|
public func run(_ options: VendorOptions) -> Result<(), MASError> {
|
|
|
|
do {
|
|
|
|
guard let result = try storeSearch.lookup(app: options.appId)
|
|
|
|
else {
|
|
|
|
print("No results found")
|
|
|
|
return .failure(.noSearchResultsFound)
|
|
|
|
}
|
|
|
|
|
2019-01-12 00:11:26 +00:00
|
|
|
guard let vendorWebsite = result.sellerUrl
|
|
|
|
else { throw MASError.noVendorWebsite }
|
|
|
|
|
2019-01-04 01:19:44 +00:00
|
|
|
do {
|
2019-01-12 00:11:26 +00:00
|
|
|
try openCommand.run(arguments: vendorWebsite)
|
2019-01-04 01:19:44 +00:00
|
|
|
} catch {
|
|
|
|
printError("Unable to launch open command")
|
|
|
|
return .failure(.searchFailed)
|
|
|
|
}
|
|
|
|
if openCommand.failed {
|
|
|
|
let reason = openCommand.process.terminationReason
|
|
|
|
printError("Open failed: (\(reason)) \(openCommand.stderr)")
|
|
|
|
return .failure(.searchFailed)
|
|
|
|
}
|
2019-01-12 01:06:02 +00:00
|
|
|
} catch {
|
2019-01-05 01:05:58 +00:00
|
|
|
// Bubble up MASErrors
|
|
|
|
if let error = error as? MASError {
|
|
|
|
return .failure(error)
|
|
|
|
}
|
2019-01-04 01:19:44 +00:00
|
|
|
return .failure(.searchFailed)
|
|
|
|
}
|
|
|
|
|
|
|
|
return .success(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public struct VendorOptions: OptionsProtocol {
|
|
|
|
let appId: String
|
|
|
|
|
|
|
|
static func create(_ appId: String) -> VendorOptions {
|
|
|
|
return VendorOptions(appId: appId)
|
|
|
|
}
|
|
|
|
|
2019-01-12 01:06:02 +00:00
|
|
|
public static func evaluate(_ mode: CommandMode) -> Result<VendorOptions, CommandantError<MASError>> {
|
2019-01-04 01:19:44 +00:00
|
|
|
return create
|
2019-01-12 01:06:02 +00:00
|
|
|
<*> mode <| Argument(usage: "the app id to show the vendor's website")
|
2019-01-04 01:19:44 +00:00
|
|
|
}
|
|
|
|
}
|