From ed755cdf46fcb24890dc4053c7eb7da9db2ae58f Mon Sep 17 00:00:00 2001 From: Daniel Bayley Date: Tue, 16 Jun 2020 15:25:16 +0100 Subject: [PATCH] Add i[nstall] alias Add `i` alias for `install` command. See #281. --- MasKit/Commands/I.swift | 69 +++++++++++++++++++++++++++++++++++++++++ mas/main.swift | 1 + 2 files changed, 70 insertions(+) create mode 100644 MasKit/Commands/I.swift diff --git a/MasKit/Commands/I.swift b/MasKit/Commands/I.swift new file mode 100644 index 000000000..e8ef08952 --- /dev/null +++ b/MasKit/Commands/I.swift @@ -0,0 +1,69 @@ +// +// Install.swift +// mas-cli +// +// Created by Andrew Naylor on 21/08/2015. +// Copyright (c) 2015 Andrew Naylor. All rights reserved. +// + +import Commandant +import CommerceKit + +/// Installs previously purchased apps from the Mac App Store. +public struct InstallCommand: CommandProtocol { + public typealias Options = InstallOptions + public let verb = "i" + public let function = "Install from the Mac App Store" + + private let appLibrary: AppLibrary + + /// Public initializer. + public init() { + self.init(appLibrary: MasAppLibrary()) + } + + /// Internal initializer. + /// - Parameter appLibrary: AppLibrary manager. + init(appLibrary: AppLibrary = MasAppLibrary()) { + self.appLibrary = appLibrary + } + + /// Runs the command. + public func run(_ options: Options) -> Result<(), MASError> { + // Try to download applications with given identifiers and collect results + let downloadResults = options.appIds.compactMap { (appId) -> MASError? in + if let product = appLibrary.installedApp(forId: appId), !options.forceInstall { + printWarning("\(product.appName) is already installed") + return nil + } + + return download(appId) + } + + switch downloadResults.count { + case 0: + return .success(()) + case 1: + return .failure(downloadResults[0]) + default: + return .failure(.downloadFailed(error: nil)) + } + } +} + +public struct InstallOptions: OptionsProtocol { + let appIds: [UInt64] + let forceInstall: Bool + + public static func create(_ appIds: [Int]) -> (_ forceInstall: Bool) -> InstallOptions { + return { forceInstall in + InstallOptions(appIds: appIds.map { UInt64($0) }, forceInstall: forceInstall) + } + } + + public static func evaluate(_ mode: CommandMode) -> Result> { + return create + <*> mode <| Argument(usage: "app ID(s) to install") + <*> mode <| Switch(flag: nil, key: "force", usage: "force reinstall") + } +} diff --git a/mas/main.swift b/mas/main.swift index a477332f0..eca8fa980 100644 --- a/mas/main.swift +++ b/mas/main.swift @@ -23,6 +23,7 @@ registry.register(AccountCommand()) registry.register(HomeCommand()) registry.register(InfoCommand()) registry.register(InstallCommand()) +registry.register(ICommand()) registry.register(PurchaseCommand()) registry.register(ListCommand()) registry.register(LuckyCommand())