mirror of
https://github.com/mas-cli/mas
synced 2024-11-22 19:43:09 +00:00
✨ Add uninstall command
This commit is contained in:
parent
b22392729d
commit
be74199b9f
5 changed files with 170 additions and 1 deletions
124
App/Commands/Uninstall.swift
Normal file
124
App/Commands/Uninstall.swift
Normal file
|
@ -0,0 +1,124 @@
|
|||
//
|
||||
// Upgrade.swift
|
||||
// mas-cli
|
||||
//
|
||||
// Created by Ben Chatelain on 2018-12-27.
|
||||
// Copyright © 2015 Andrew Naylor. All rights reserved.
|
||||
//
|
||||
|
||||
import Commandant
|
||||
import Result
|
||||
import CommerceKit
|
||||
|
||||
/// Command which uninstalls apps managed by the Mac App Store. Relies on the "trash" command from Homebrew.
|
||||
/// Trash requires el_capitan or higher for core bottles:
|
||||
/// https://github.com/Homebrew/homebrew-core/blob/master/Formula/trash.rb
|
||||
public struct UninstallCommand: CommandProtocol {
|
||||
public typealias Options = UninstallOptions
|
||||
public let verb = "uninstall"
|
||||
public let function = "Uninstall apps installed from the Mac App Store"
|
||||
|
||||
public init() {}
|
||||
|
||||
/// 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 = installedApp(appId: appId) else {
|
||||
return .failure(.notInstalled)
|
||||
}
|
||||
|
||||
if options.dryRun {
|
||||
printInfo("\(product.appName) \(product.bundlePath)")
|
||||
printInfo("(not removed, dry run)")
|
||||
|
||||
return .success(())
|
||||
}
|
||||
|
||||
// Use the bundle path to delete the app
|
||||
let status = trash(path: product.bundlePath)
|
||||
if status {
|
||||
return .success(())
|
||||
} else {
|
||||
return .failure(.searchFailed)
|
||||
}
|
||||
}
|
||||
|
||||
/// Finds an app by ID from the set of installed apps?
|
||||
///
|
||||
/// - Parameter appId: MAS ID for app.
|
||||
/// - Returns: Software Product of app if found; nil otherwise.
|
||||
func installedApp(appId: UInt64) -> CKSoftwareProduct? {
|
||||
let appId = NSNumber(value: appId)
|
||||
|
||||
let softwareMap = CKSoftwareMap.shared()
|
||||
return softwareMap.allProducts()?.first { $0.itemIdentifier == appId }
|
||||
}
|
||||
|
||||
/// Runs the trash command in another process.
|
||||
///
|
||||
/// - Parameter path: Absolute path to the application bundle to uninstall.
|
||||
/// - Returns: true on success; fail on error
|
||||
func trash(path: String) -> Bool {
|
||||
let binaryPath = "/usr/local/bin/trash"
|
||||
let process = Process()
|
||||
let stdout = Pipe()
|
||||
let stderr = Pipe()
|
||||
|
||||
process.standardOutput = stdout
|
||||
process.standardError = stderr
|
||||
process.arguments = [path]
|
||||
|
||||
if #available(OSX 10.13, *) {
|
||||
process.executableURL = URL(fileURLWithPath: binaryPath)
|
||||
do {
|
||||
try process.run()
|
||||
} catch {
|
||||
printInfo("Unable to launch trash command")
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
process.launchPath = binaryPath
|
||||
process.launch()
|
||||
}
|
||||
|
||||
// process.terminationHandler = { (process) in
|
||||
// print("\ndidFinish: \(!process.isRunning)")
|
||||
// }
|
||||
|
||||
process.waitUntilExit()
|
||||
|
||||
if process.terminationStatus == 0 {
|
||||
return true
|
||||
} else {
|
||||
let reason = process.terminationReason
|
||||
let output = stderr.fileHandleForReading.readDataToEndOfFile()
|
||||
printInfo("Uninstall failed: \(reason)\n\(String(data: output, encoding: String.Encoding.utf8)!)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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
|
||||
return UninstallOptions(appId: appId, dryRun: dryRun)
|
||||
}
|
||||
}
|
||||
|
||||
public static func evaluate(_ m: CommandMode) -> Result<UninstallOptions, CommandantError<MASError>> {
|
||||
return create
|
||||
<*> m <| Argument(usage: "ID of app to uninstall")
|
||||
<*> m <| Switch(flag: nil, key: "dry-run", usage: "dry run")
|
||||
}
|
||||
}
|
|
@ -22,6 +22,9 @@ public enum MASError: Error, CustomStringConvertible, Equatable {
|
|||
case searchFailed
|
||||
case noSearchResultsFound
|
||||
|
||||
case notInstalled
|
||||
case uninstallFailed
|
||||
|
||||
public var description: String {
|
||||
switch self {
|
||||
case .notSignedIn:
|
||||
|
@ -67,7 +70,13 @@ public enum MASError: Error, CustomStringConvertible, Equatable {
|
|||
return "Search failed"
|
||||
|
||||
case .noSearchResultsFound:
|
||||
return "No results found"
|
||||
return "No results found"
|
||||
|
||||
case .notInstalled:
|
||||
return "Not installed"
|
||||
|
||||
case .uninstallFailed:
|
||||
return "Uninstall failed"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ registry.register(ResetCommand())
|
|||
registry.register(SearchCommand())
|
||||
registry.register(SignInCommand())
|
||||
registry.register(SignOutCommand())
|
||||
registry.register(UninstallCommand())
|
||||
registry.register(UpgradeCommand())
|
||||
registry.register(VersionCommand())
|
||||
registry.register(helpCommand)
|
||||
|
|
25
MasKitTests/ListCommandSpec.swift
Normal file
25
MasKitTests/ListCommandSpec.swift
Normal file
|
@ -0,0 +1,25 @@
|
|||
//
|
||||
// ListCommandSpec.swift
|
||||
// MasKitTests
|
||||
//
|
||||
// Created by Ben Chatelain on 2018-12-27.
|
||||
// Copyright © 2018 mas-cli. All rights reserved.
|
||||
//
|
||||
|
||||
@testable import MasKit
|
||||
import Result
|
||||
import Quick
|
||||
import Nimble
|
||||
|
||||
class ListCommandSpec: QuickSpec {
|
||||
override func spec() {
|
||||
describe("list command") {
|
||||
it("lists stuff") {
|
||||
let list = ListCommand()
|
||||
let result = list.run(ListCommand.Options())
|
||||
print(result)
|
||||
// expect(result).to(beSuccess())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,6 +21,8 @@
|
|||
B5552937219A23FF00ACB4CA /* Quick.framework in Copy Carthage Frameworks */ = {isa = PBXBuildFile; fileRef = 90CB406A213F4DDD0044E445 /* Quick.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
|
||||
B5793E29219BDD4800135B39 /* JSON in Resources */ = {isa = PBXBuildFile; fileRef = B5793E28219BDD4800135B39 /* JSON */; };
|
||||
B5793E2B219BE0CD00135B39 /* MockURLSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5793E2A219BE0CD00135B39 /* MockURLSession.swift */; };
|
||||
B594B12021D53A8200F3AC59 /* Uninstall.swift in Sources */ = {isa = PBXBuildFile; fileRef = B594B11F21D53A8200F3AC59 /* Uninstall.swift */; };
|
||||
B594B12221D5416100F3AC59 /* ListCommandSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = B594B12121D5416100F3AC59 /* ListCommandSpec.swift */; };
|
||||
ED031A7C1B5127C00097692E /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED031A7B1B5127C00097692E /* main.swift */; };
|
||||
F83213892173D3E1008BA8A0 /* CKAccountStore.h in Headers */ = {isa = PBXBuildFile; fileRef = F8FB719B20F2EC4500F56FDC /* CKAccountStore.h */; };
|
||||
F832138A2173D3E1008BA8A0 /* CKDownloadQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = F8FB719C20F2EC4500F56FDC /* CKDownloadQueue.h */; };
|
||||
|
@ -156,6 +158,8 @@
|
|||
B555292C219A1FE700ACB4CA /* SearchSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchSpec.swift; sourceTree = "<group>"; };
|
||||
B5793E28219BDD4800135B39 /* JSON */ = {isa = PBXFileReference; lastKnownFileType = folder; path = JSON; sourceTree = "<group>"; };
|
||||
B5793E2A219BE0CD00135B39 /* MockURLSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockURLSession.swift; sourceTree = "<group>"; };
|
||||
B594B11F21D53A8200F3AC59 /* Uninstall.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Uninstall.swift; sourceTree = "<group>"; };
|
||||
B594B12121D5416100F3AC59 /* ListCommandSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListCommandSpec.swift; sourceTree = "<group>"; };
|
||||
ED031A781B5127C00097692E /* mas */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = mas; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
ED031A7B1B5127C00097692E /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
|
||||
ED0F237E1B87522400AE40CD /* Install.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Install.swift; sourceTree = "<group>"; };
|
||||
|
@ -311,6 +315,7 @@
|
|||
693A98981CBFFA760004D3B4 /* Search.swift */,
|
||||
EDC90B641C70045E0019E396 /* SignIn.swift */,
|
||||
EDE296521C700F4300554778 /* SignOut.swift */,
|
||||
B594B11F21D53A8200F3AC59 /* Uninstall.swift */,
|
||||
EDD3B3621C34709400B56B88 /* Upgrade.swift */,
|
||||
EDB6CE8B1BAEC3D400648B4D /* Version.swift */,
|
||||
);
|
||||
|
@ -355,6 +360,7 @@
|
|||
children = (
|
||||
F8FB716120F2B41400F56FDC /* Info.plist */,
|
||||
B5793E28219BDD4800135B39 /* JSON */,
|
||||
B594B12121D5416100F3AC59 /* ListCommandSpec.swift */,
|
||||
B555292A219A1CB200ACB4CA /* MASErrorTestCase.swift */,
|
||||
B5793E2A219BE0CD00135B39 /* MockURLSession.swift */,
|
||||
B555292C219A1FE700ACB4CA /* SearchSpec.swift */,
|
||||
|
@ -606,6 +612,7 @@
|
|||
F8FB716C20F2B4DD00F56FDC /* PurchaseDownloadObserver.swift in Sources */,
|
||||
F8FB717520F2B4DD00F56FDC /* Reset.swift in Sources */,
|
||||
F8FB717620F2B4DD00F56FDC /* Search.swift in Sources */,
|
||||
B594B12021D53A8200F3AC59 /* Uninstall.swift in Sources */,
|
||||
F8FB717720F2B4DD00F56FDC /* SignIn.swift in Sources */,
|
||||
F8FB717820F2B4DD00F56FDC /* SignOut.swift in Sources */,
|
||||
F8FB716D20F2B4DD00F56FDC /* SSPurchase.swift in Sources */,
|
||||
|
@ -622,6 +629,7 @@
|
|||
files = (
|
||||
B5793E2B219BE0CD00135B39 /* MockURLSession.swift in Sources */,
|
||||
B555292B219A1CB200ACB4CA /* MASErrorTestCase.swift in Sources */,
|
||||
B594B12221D5416100F3AC59 /* ListCommandSpec.swift in Sources */,
|
||||
B555292D219A1FE700ACB4CA /* SearchSpec.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
@ -775,6 +783,7 @@
|
|||
"$(inherited)",
|
||||
"$(PROJECT_DIR)/Carthage/Build/Mac",
|
||||
);
|
||||
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
|
||||
INSTALL_PATH = /bin;
|
||||
LD_RUNPATH_SEARCH_PATHS = "@executable_path/. @executable_path/MasKit.framework/Versions/Current/Frameworks /usr/local/Frameworks /usr/local/Frameworks/MasKit.framework/Versions/Current/Frameworks $(inherited)";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mphys.mas-cli";
|
||||
|
@ -796,6 +805,7 @@
|
|||
"$(inherited)",
|
||||
"$(PROJECT_DIR)/Carthage/Build/Mac",
|
||||
);
|
||||
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
|
||||
INSTALL_PATH = /bin;
|
||||
LD_RUNPATH_SEARCH_PATHS = "@executable_path/. @executable_path/MasKit.framework/Versions/Current/Frameworks /usr/local/Frameworks /usr/local/Frameworks/MasKit.framework/Versions/Current/Frameworks $(inherited)";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mphys.mas-cli";
|
||||
|
|
Loading…
Reference in a new issue