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
|
2021-04-26 23:44:17 +00:00
|
|
|
import Foundation
|
2019-01-04 01:15:50 +00:00
|
|
|
|
2019-01-12 07:00:28 +00:00
|
|
|
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
|
|
|
|
|
2021-04-22 01:05:36 +00:00
|
|
|
public init() {
|
|
|
|
self.init(
|
|
|
|
storeSearch: MasStoreSearch(),
|
|
|
|
openCommand: OpenSystemCommand()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-01-04 01:15:50 +00:00
|
|
|
/// Designated initializer.
|
2021-04-22 01:05:36 +00:00
|
|
|
init(
|
2021-03-22 05:25:18 +00:00
|
|
|
storeSearch: StoreSearch = MasStoreSearch(),
|
|
|
|
openCommand: ExternalCommand = OpenSystemCommand()
|
|
|
|
) {
|
2019-01-04 01:15:50 +00:00
|
|
|
self.storeSearch = storeSearch
|
2019-01-30 06:15:24 +00:00
|
|
|
systemOpen = openCommand
|
2019-01-04 01:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs the command.
|
2021-03-22 05:46:17 +00:00
|
|
|
public func run(_ options: OpenOptions) -> Result<Void, MASError> {
|
2019-01-04 01:15:50 +00:00
|
|
|
do {
|
2019-01-12 07:00:28 +00:00
|
|
|
if options.appId == markerValue {
|
|
|
|
// If no app ID is given, just open the MAS GUI app
|
|
|
|
try systemOpen.run(arguments: masScheme + "://")
|
|
|
|
return .success(())
|
2019-01-12 06:30:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 05:06:25 +00:00
|
|
|
guard let appId = Int(options.appId)
|
2021-03-22 05:25:18 +00:00
|
|
|
else {
|
2021-11-02 04:10:56 +00:00
|
|
|
printError("Invalid app ID")
|
2021-03-22 05:25:18 +00:00
|
|
|
return .failure(.noSearchResultsFound)
|
2019-01-18 05:06:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 22:08:56 +00:00
|
|
|
guard let result = try storeSearch.lookup(app: appId).wait()
|
2021-03-22 05:25:18 +00:00
|
|
|
else {
|
|
|
|
return .failure(.noSearchResultsFound)
|
2019-01-04 01:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
guard var url = URLComponents(string: result.trackViewUrl)
|
2021-03-22 05:25:18 +00:00
|
|
|
else {
|
|
|
|
return .failure(.searchFailed)
|
2019-01-04 01:15:50 +00:00
|
|
|
}
|
2019-01-12 07:00:28 +00:00
|
|
|
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 {
|
2019-01-12 07:00:28 +00:00
|
|
|
var appId: String
|
2019-01-04 01:15:50 +00:00
|
|
|
|
2019-01-12 07:00:28 +00:00
|
|
|
static func create(_ appId: String) -> OpenOptions {
|
2021-03-22 05:46:17 +00:00
|
|
|
OpenOptions(appId: appId)
|
2019-01-04 01:15:50 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 01:06:02 +00:00
|
|
|
public static func evaluate(_ mode: CommandMode) -> Result<OpenOptions, CommandantError<MASError>> {
|
2021-03-22 05:46:17 +00:00
|
|
|
create
|
2019-01-18 05:06:25 +00:00
|
|
|
<*> mode <| Argument(defaultValue: markerValue, usage: "the app ID")
|
2019-01-04 01:15:50 +00:00
|
|
|
}
|
|
|
|
}
|