From 55d7e6e393baf7a9185e2260c5f32898baa2ef4c Mon Sep 17 00:00:00 2001 From: Daniel Bayley Date: Tue, 16 Jun 2020 15:29:38 +0100 Subject: [PATCH] Add remove alias Add `remove` alias for `uninstall` command. See #281. --- MasKit/Commands/Remove.swift | 80 ++++++++++++++++++++++++++++++++++++ mas/main.swift | 1 + 2 files changed, 81 insertions(+) create mode 100644 MasKit/Commands/Remove.swift diff --git a/MasKit/Commands/Remove.swift b/MasKit/Commands/Remove.swift new file mode 100644 index 000000000..4242b3d6f --- /dev/null +++ b/MasKit/Commands/Remove.swift @@ -0,0 +1,80 @@ +// +// Upgrade.swift +// mas-cli +// +// Created by Ben Chatelain on 2018-12-27. +// Copyright © 2015 Andrew Naylor. All rights reserved. +// + +import Commandant +import CommerceKit +import StoreFoundation + +/// Command which uninstalls apps managed by the Mac App Store. +public struct UninstallCommand: CommandProtocol { + public typealias Options = UninstallOptions + public let verb = "remove" + public let function = "Uninstall app installed from the Mac App Store" + + private let appLibrary: AppLibrary + + /// Public initializer. + /// - Parameter appLibrary: AppLibrary manager. + public init() { + self.init(appLibrary: MasAppLibrary()) + } + + /// Internal initializer. + /// - Parameter appLibrary: AppLibrary manager. + init(appLibrary: AppLibrary = MasAppLibrary()) { + self.appLibrary = appLibrary + } + + /// Runs the uninstall command. + /// + /// - Parameter options: UninstallOptions (arguments) for this command + /// - Returns: Success or an error. + public func run(_ options: Options) -> Result<(), MASError> { + let appId = UInt64(options.appId) + + guard let product = appLibrary.installedApp(forId: appId) else { + return .failure(.notInstalled) + } + + if options.dryRun { + printInfo("\(product.appName) \(product.bundlePath)") + printInfo("(not removed, dry run)") + + return .success(()) + } + + do { + try appLibrary.uninstallApp(app: product) + } catch { + return .failure(.uninstallFailed) + } + + return .success(()) + } +} + +/// 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 + UninstallOptions(appId: appId, dryRun: dryRun) + } + } + + public static func evaluate(_ mode: CommandMode) -> Result> { + return create + <*> mode <| Argument(usage: "ID of app to uninstall") + <*> mode <| Switch(flag: nil, key: "dry-run", usage: "dry run") + } +} diff --git a/mas/main.swift b/mas/main.swift index bef442c34..096fb28ed 100644 --- a/mas/main.swift +++ b/mas/main.swift @@ -35,6 +35,7 @@ registry.register(SearchCommand()) registry.register(SignInCommand()) registry.register(SignOutCommand()) registry.register(UninstallCommand()) +registry.register(RemoveCommand()) registry.register(UpgradeCommand()) registry.register(VendorCommand()) registry.register(VersionCommand())