mas/MasKit/Commands/Open.swift

95 lines
2.9 KiB
Swift
Raw Normal View History

2019-01-04 01:15:50 +00:00
//
// Open.swift
// mas-cli
//
// Created by Ben Chatelain on 2018-12-29.
// Copyright © 2016 mas-cli. All rights reserved.
//
import Commandant
import Result
private let markerValue = "appstore"
private let masScheme = "macappstore"
2019-01-04 01:15:50 +00:00
/// Opens app page in MAS app. Uses the iTunes Lookup API:
/// https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/#lookup
public struct OpenCommand: CommandProtocol {
public typealias Options = OpenOptions
public let verb = "open"
public let function = "Opens app page in AppStore.app"
private let storeSearch: StoreSearch
private var systemOpen: ExternalCommand
/// Designated initializer.
public init(storeSearch: StoreSearch = MasStoreSearch(),
openCommand: ExternalCommand = OpenSystemCommand()) {
self.storeSearch = storeSearch
self.systemOpen = openCommand
}
/// Runs the command.
public func run(_ options: OpenOptions) -> Result<(), MASError> {
do {
if options.appId == markerValue {
// If no app ID is given, just open the MAS GUI app
try systemOpen.run(arguments: masScheme + "://")
return .success(())
}
guard let appId = Int(options.appId)
else {
print("Invalid app ID")
return .failure(.noSearchResultsFound)
}
guard let result = try storeSearch.lookup(app: appId)
2019-01-04 01:15:50 +00:00
else {
print("No results found")
return .failure(.noSearchResultsFound)
}
guard var url = URLComponents(string: result.trackViewUrl)
else {
return .failure(.searchFailed)
}
url.scheme = masScheme
2019-01-04 01:15:50 +00:00
do {
try systemOpen.run(arguments: url.string!)
} catch {
printError("Unable to launch open command")
return .failure(.searchFailed)
}
if systemOpen.failed {
let reason = systemOpen.process.terminationReason
printError("Open failed: (\(reason)) \(systemOpen.stderr)")
return .failure(.searchFailed)
}
2019-01-12 01:06:02 +00:00
} catch {
2019-01-05 01:01:22 +00:00
// Bubble up MASErrors
if let error = error as? MASError {
return .failure(error)
}
2019-01-04 01:15:50 +00:00
return .failure(.searchFailed)
}
return .success(())
}
}
public struct OpenOptions: OptionsProtocol {
var appId: String
2019-01-04 01:15:50 +00:00
static func create(_ appId: String) -> OpenOptions {
2019-01-04 01:15:50 +00:00
return OpenOptions(appId: appId)
}
2019-01-12 01:06:02 +00:00
public static func evaluate(_ mode: CommandMode) -> Result<OpenOptions, CommandantError<MASError>> {
2019-01-04 01:15:50 +00:00
return create
<*> mode <| Argument(defaultValue: markerValue, usage: "the app ID")
2019-01-04 01:15:50 +00:00
}
}