Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aliases #283

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions MasKit/Commands/I.swift
Original file line number Diff line number Diff line change
@@ -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<InstallOptions, CommandantError<MASError>> {
return create
<*> mode <| Argument(usage: "app ID(s) to install")
<*> mode <| Switch(flag: nil, key: "force", usage: "force reinstall")
}
}
47 changes: 47 additions & 0 deletions MasKit/Commands/Ls.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// List.swift
// mas-cli
//
// Created by Andrew Naylor on 21/08/2015.
// Copyright (c) 2015 Andrew Naylor. All rights reserved.
//

import Commandant

/// Command which lists all installed apps.
public struct ListCommand: CommandProtocol {
public typealias Options = NoOptions<MASError>
public let verb = "ls"
public let function = "Lists apps from the Mac App Store which are currently installed"

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 command.
public func run(_: Options) -> Result<(), MASError> {
let products = appLibrary.installedApps
if products.isEmpty {
print("No installed apps found")
return .success(())
}
for product in products {
var appName = product.appName
if appName == "" {
appName = product.bundleIdentifier
}
print("\(product.itemIdentifier) \(appName) (\(product.bundleVersion))")
}
return .success(())
}
}
80 changes: 80 additions & 0 deletions MasKit/Commands/Remove.swift
Original file line number Diff line number Diff line change
@@ -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<UninstallOptions, CommandantError<MASError>> {
return create
<*> mode <| Argument(usage: "ID of app to uninstall")
<*> mode <| Switch(flag: nil, key: "dry-run", usage: "dry run")
}
}
80 changes: 80 additions & 0 deletions MasKit/Commands/Rm.swift
Original file line number Diff line number Diff line change
@@ -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 = "rm"
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<UninstallOptions, CommandantError<MASError>> {
return create
<*> mode <| Argument(usage: "ID of app to uninstall")
<*> mode <| Switch(flag: nil, key: "dry-run", usage: "dry run")
}
}
46 changes: 46 additions & 0 deletions MasKit/Commands/Update.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Update.swift
// mas-cli
//
// Created by Andrew Naylor on 30/12/2015.
// Copyright © 2015 Andrew Naylor. All rights reserved.
//

import Commandant
import Foundation

/// Command which updates mas.
public struct UpdateCommand: CommandProtocol {
public typealias Options = NoOptions<MASError>
public let verb = "update"
public let function = "Update MAS"

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 command.
public func run(_: Options) -> Result<(), MASError> {
print("Updating MAS…")

let task = Process()
task.launchPath = getenv("HOMEBREW_BREW_FILE")
task.arguments = ["upgrade", "mas"]
//let pipe = Pipe()
//task.standardOutput = pipe
//task.standardError = pipe
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
}
Loading