Skip to content

Commit

Permalink
Merge pull request #477 from mas-cli/errors
Browse files Browse the repository at this point in the history
🔇 Errors
  • Loading branch information
phatblat authored Oct 8, 2022
2 parents 4cba212 + 036dc05 commit 4c044d8
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
"repositoryURL": "https://github.com/apple/swift-syntax",
"state": {
"branch": null,
"revision": "04d4497be6b88e524a71778d828790e9589ae1c4",
"version": "0.50700.0"
"revision": "72d3da66b085c2299dd287c2be3b92b5ebd226de",
"version": "0.50700.1"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Sources/MasKit/Commands/Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct AccountCommand: CommandProtocol {
if let account = ISStoreAccount.primaryAccount {
print(String(describing: account.identifier))
} else {
print("Not signed in")
printError("Not signed in")
return .failure(.notSignedIn)
}
return .success(())
Expand Down
1 change: 0 additions & 1 deletion Sources/MasKit/Commands/Home.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public struct HomeCommand: CommandProtocol {
public func run(_ options: HomeOptions) -> Result<Void, MASError> {
do {
guard let result = try storeSearch.lookup(app: options.appId).wait() else {
print("No results found")
return .failure(.noSearchResultsFound)
}

Expand Down
1 change: 0 additions & 1 deletion Sources/MasKit/Commands/Info.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public struct InfoCommand: CommandProtocol {
public func run(_ options: InfoOptions) -> Result<Void, MASError> {
do {
guard let result = try storeSearch.lookup(app: options.appId).wait() else {
print("No results found")
return .failure(.noSearchResultsFound)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/MasKit/Commands/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public struct ListCommand: CommandProtocol {
public func run(_: Options) -> Result<Void, MASError> {
let products = appLibrary.installedApps
if products.isEmpty {
print("No installed apps found")
printError("No installed apps found")
return .success(())
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/MasKit/Commands/Lucky.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public struct LuckyCommand: CommandProtocol {
do {
let results = try storeSearch.search(for: options.appName).wait()
guard let result = results.first else {
print("No results found")
printError("No results found")
return .failure(.noSearchResultsFound)
}

Expand Down
1 change: 0 additions & 1 deletion Sources/MasKit/Commands/Open.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public struct OpenCommand: CommandProtocol {

guard let result = try storeSearch.lookup(app: appId).wait()
else {
print("No results found")
return .failure(.noSearchResultsFound)
}

Expand Down
31 changes: 24 additions & 7 deletions Sources/MasKit/Commands/Outdated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import enum Swift.Result
/// Command which displays a list of installed apps which have available updates
/// ready to be installed from the Mac App Store.
public struct OutdatedCommand: CommandProtocol {
public typealias Options = NoOptions<MASError>
public typealias Options = OutdatedOptions
public let verb = "outdated"
public let function = "Lists pending updates from the Mac App Store"

Expand All @@ -36,17 +36,19 @@ public struct OutdatedCommand: CommandProtocol {
}

/// Runs the command.
public func run(_: Options) -> Result<Void, MASError> {
public func run(_ options: Options) -> Result<Void, MASError> {
let promises = appLibrary.installedApps.map { installedApp in
firstly {
storeSearch.lookup(app: installedApp.itemIdentifier.intValue)
}.done { storeApp in
guard let storeApp = storeApp else {
printWarning(
"""
Identifier \(installedApp.itemIdentifier) not found in store. \
Was expected to identify \(installedApp.appName).
""")
if options.verbose {
printWarning(
"""
Identifier \(installedApp.itemIdentifier) not found in store. \
Was expected to identify \(installedApp.appName).
""")
}
return
}

Expand All @@ -70,3 +72,18 @@ public struct OutdatedCommand: CommandProtocol {
}.wait()
}
}

public struct OutdatedOptions: OptionsProtocol {
public typealias ClientError = MASError

let verbose: Bool

static func create(verbose: Bool) -> OutdatedOptions {
OutdatedOptions(verbose: verbose)
}

public static func evaluate(_ mode: CommandMode) -> Result<OutdatedOptions, CommandantError<MASError>> {
create
<*> mode <| Switch(flag: nil, key: "verbose", usage: "Show warnings about apps")
}
}
1 change: 0 additions & 1 deletion Sources/MasKit/Commands/Search.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public struct SearchCommand: CommandProtocol {
do {
let results = try storeSearch.search(for: options.appName).wait()
if results.isEmpty {
print("No results found")
return .failure(.noSearchResultsFound)
}

Expand Down
1 change: 0 additions & 1 deletion Sources/MasKit/Commands/Vendor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public struct VendorCommand: CommandProtocol {
do {
guard let result = try storeSearch.lookup(app: options.appId).wait()
else {
print("No results found")
return .failure(.noSearchResultsFound)
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/MasKit/Formatters/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ extension FileHandle: TextOutputStream {
}
}

/// Prints a message to stdout prefixed with a blue arrow.
func printInfo(_ message: String) {
guard isatty(fileno(stdout)) != 0 else {
print("==> \(message)")
Expand All @@ -89,6 +90,7 @@ func printInfo(_ message: String) {
print("\(csi)1;34m==>\(csi)0m \(csi)1m\(message)\(csi)0m")
}

/// Prints a message to stderr prefixed with "Warning:" underlined in yellow.
public func printWarning(_ message: String) {
guard isatty(fileno(stderr)) != 0 else {
print("Warning: \(message)", to: &standardError)
Expand All @@ -99,6 +101,7 @@ public func printWarning(_ message: String) {
print("\(csi)4;33mWarning:\(csi)0m \(message)", to: &standardError)
}

/// Prints a message to stderr prefixed with "Error:" underlined in red.
public func printError(_ message: String) {
guard isatty(fileno(stderr)) != 0 else {
print("Error: \(message)", to: &standardError)
Expand All @@ -109,6 +112,7 @@ public func printError(_ message: String) {
print("\(csi)4;31mError:\(csi)0m \(message)", to: &standardError)
}

/// Flushes stdout.
func clearLine() {
guard isatty(fileno(stdout)) != 0 else {
return
Expand Down
4 changes: 2 additions & 2 deletions Tests/MasKitTests/Commands/OutdatedCommandSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public class OutdatedCommandSpec: QuickSpec {
describe("outdated command") {
it("displays apps with pending updates") {
let cmd = OutdatedCommand()
let result = cmd.run(OutdatedCommand.Options())
let result = cmd.run(OutdatedCommand.Options(verbose: true))
print(result)
// expect(result).to(beSuccess())
expect(result).to(beSuccess())
}
}
}
Expand Down

0 comments on commit 4c044d8

Please sign in to comment.