2018-12-27 21:01:48 +00:00
|
|
|
//
|
|
|
|
// Upgrade.swift
|
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Ben Chatelain on 2018-12-27.
|
|
|
|
// Copyright © 2015 Andrew Naylor. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Commandant
|
|
|
|
import CommerceKit
|
2019-01-30 06:15:24 +00:00
|
|
|
import Result
|
2018-12-27 22:24:20 +00:00
|
|
|
import StoreFoundation
|
2018-12-27 21:01:48 +00:00
|
|
|
|
2018-12-27 23:07:35 +00:00
|
|
|
/// Command which uninstalls apps managed by the Mac App Store.
|
2018-12-27 21:01:48 +00:00
|
|
|
public struct UninstallCommand: CommandProtocol {
|
|
|
|
public typealias Options = UninstallOptions
|
|
|
|
public let verb = "uninstall"
|
2018-12-27 21:32:58 +00:00
|
|
|
public let function = "Uninstall app installed from the Mac App Store"
|
2018-12-27 21:01:48 +00:00
|
|
|
|
2018-12-27 22:24:20 +00:00
|
|
|
private let appLibrary: AppLibrary
|
2018-12-27 21:01:48 +00:00
|
|
|
|
2018-12-27 22:24:20 +00:00
|
|
|
/// Designated initializer.
|
|
|
|
///
|
|
|
|
/// - Parameter appLibrary: <#appLibrary description#>
|
|
|
|
public init(appLibrary: AppLibrary = MasAppLibrary()) {
|
|
|
|
self.appLibrary = appLibrary
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs the uninstall command.
|
2018-12-27 21:01:48 +00:00
|
|
|
///
|
|
|
|
/// - Parameter options: UninstallOptions (arguments) for this command
|
|
|
|
/// - Returns: Success or an error.
|
|
|
|
public func run(_ options: Options) -> Result<(), MASError> {
|
|
|
|
let appId = UInt64(options.appId)
|
|
|
|
|
2018-12-28 22:08:31 +00:00
|
|
|
guard let product = appLibrary.installedApp(forId: appId) else {
|
2018-12-27 21:01:48 +00:00
|
|
|
return .failure(.notInstalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.dryRun {
|
|
|
|
printInfo("\(product.appName) \(product.bundlePath)")
|
|
|
|
printInfo("(not removed, dry run)")
|
|
|
|
|
|
|
|
return .success(())
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:07:35 +00:00
|
|
|
do {
|
|
|
|
try appLibrary.uninstallApp(app: product)
|
2019-01-12 01:06:02 +00:00
|
|
|
} catch {
|
2018-12-27 23:07:35 +00:00
|
|
|
return .failure(.uninstallFailed)
|
2018-12-27 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 23:07:35 +00:00
|
|
|
return .success(())
|
2018-12-27 21:01:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Options for the uninstall command.
|
|
|
|
public struct UninstallOptions: OptionsProtocol {
|
|
|
|
/// Numeric app ID
|
|
|
|
let appId: Int
|
|
|
|
|
|
|
|
/// Flag indicating that removal shouldn't be performed
|
|
|
|
let dryRun: Bool
|
|
|
|
|
|
|
|
static func create(_ appId: Int) -> (_ dryRun: Bool) -> UninstallOptions {
|
|
|
|
return { dryRun in
|
2019-01-30 06:15:24 +00:00
|
|
|
UninstallOptions(appId: appId, dryRun: dryRun)
|
2018-12-27 21:01:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 01:06:02 +00:00
|
|
|
public static func evaluate(_ mode: CommandMode) -> Result<UninstallOptions, CommandantError<MASError>> {
|
2018-12-27 21:01:48 +00:00
|
|
|
return create
|
2019-01-12 01:06:02 +00:00
|
|
|
<*> mode <| Argument(usage: "ID of app to uninstall")
|
|
|
|
<*> mode <| Switch(flag: nil, key: "dry-run", usage: "dry run")
|
2018-12-27 21:01:48 +00:00
|
|
|
}
|
|
|
|
}
|