Merge pull request #477 from mas-cli/errors

🔇 Errors
This commit is contained in:
Ben Chatelain 2022-10-08 17:17:03 -06:00 committed by GitHub
commit 4c044d8b64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 35 additions and 19 deletions

View file

@ -78,8 +78,8 @@
"repositoryURL": "https://github.com/apple/swift-syntax", "repositoryURL": "https://github.com/apple/swift-syntax",
"state": { "state": {
"branch": null, "branch": null,
"revision": "04d4497be6b88e524a71778d828790e9589ae1c4", "revision": "72d3da66b085c2299dd287c2be3b92b5ebd226de",
"version": "0.50700.0" "version": "0.50700.1"
} }
}, },
{ {

View file

@ -27,7 +27,7 @@ public struct AccountCommand: CommandProtocol {
if let account = ISStoreAccount.primaryAccount { if let account = ISStoreAccount.primaryAccount {
print(String(describing: account.identifier)) print(String(describing: account.identifier))
} else { } else {
print("Not signed in") printError("Not signed in")
return .failure(.notSignedIn) return .failure(.notSignedIn)
} }
return .success(()) return .success(())

View file

@ -39,7 +39,6 @@ public struct HomeCommand: CommandProtocol {
public func run(_ options: HomeOptions) -> Result<Void, MASError> { public func run(_ options: HomeOptions) -> Result<Void, MASError> {
do { do {
guard let result = try storeSearch.lookup(app: options.appId).wait() else { guard let result = try storeSearch.lookup(app: options.appId).wait() else {
print("No results found")
return .failure(.noSearchResultsFound) return .failure(.noSearchResultsFound)
} }

View file

@ -30,7 +30,6 @@ public struct InfoCommand: CommandProtocol {
public func run(_ options: InfoOptions) -> Result<Void, MASError> { public func run(_ options: InfoOptions) -> Result<Void, MASError> {
do { do {
guard let result = try storeSearch.lookup(app: options.appId).wait() else { guard let result = try storeSearch.lookup(app: options.appId).wait() else {
print("No results found")
return .failure(.noSearchResultsFound) return .failure(.noSearchResultsFound)
} }

View file

@ -32,7 +32,7 @@ public struct ListCommand: CommandProtocol {
public func run(_: Options) -> Result<Void, MASError> { public func run(_: Options) -> Result<Void, MASError> {
let products = appLibrary.installedApps let products = appLibrary.installedApps
if products.isEmpty { if products.isEmpty {
print("No installed apps found") printError("No installed apps found")
return .success(()) return .success(())
} }

View file

@ -47,7 +47,7 @@ public struct LuckyCommand: CommandProtocol {
do { do {
let results = try storeSearch.search(for: options.appName).wait() let results = try storeSearch.search(for: options.appName).wait()
guard let result = results.first else { guard let result = results.first else {
print("No results found") printError("No results found")
return .failure(.noSearchResultsFound) return .failure(.noSearchResultsFound)
} }

View file

@ -56,7 +56,6 @@ public struct OpenCommand: CommandProtocol {
guard let result = try storeSearch.lookup(app: appId).wait() guard let result = try storeSearch.lookup(app: appId).wait()
else { else {
print("No results found")
return .failure(.noSearchResultsFound) return .failure(.noSearchResultsFound)
} }

View file

@ -15,7 +15,7 @@ import enum Swift.Result
/// Command which displays a list of installed apps which have available updates /// Command which displays a list of installed apps which have available updates
/// ready to be installed from the Mac App Store. /// ready to be installed from the Mac App Store.
public struct OutdatedCommand: CommandProtocol { public struct OutdatedCommand: CommandProtocol {
public typealias Options = NoOptions<MASError> public typealias Options = OutdatedOptions
public let verb = "outdated" public let verb = "outdated"
public let function = "Lists pending updates from the Mac App Store" public let function = "Lists pending updates from the Mac App Store"
@ -36,17 +36,19 @@ public struct OutdatedCommand: CommandProtocol {
} }
/// Runs the command. /// 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 let promises = appLibrary.installedApps.map { installedApp in
firstly { firstly {
storeSearch.lookup(app: installedApp.itemIdentifier.intValue) storeSearch.lookup(app: installedApp.itemIdentifier.intValue)
}.done { storeApp in }.done { storeApp in
guard let storeApp = storeApp else { guard let storeApp = storeApp else {
if options.verbose {
printWarning( printWarning(
""" """
Identifier \(installedApp.itemIdentifier) not found in store. \ Identifier \(installedApp.itemIdentifier) not found in store. \
Was expected to identify \(installedApp.appName). Was expected to identify \(installedApp.appName).
""") """)
}
return return
} }
@ -70,3 +72,18 @@ public struct OutdatedCommand: CommandProtocol {
}.wait() }.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")
}
}

View file

@ -32,7 +32,6 @@ public struct SearchCommand: CommandProtocol {
do { do {
let results = try storeSearch.search(for: options.appName).wait() let results = try storeSearch.search(for: options.appName).wait()
if results.isEmpty { if results.isEmpty {
print("No results found")
return .failure(.noSearchResultsFound) return .failure(.noSearchResultsFound)
} }

View file

@ -40,7 +40,6 @@ public struct VendorCommand: CommandProtocol {
do { do {
guard let result = try storeSearch.lookup(app: options.appId).wait() guard let result = try storeSearch.lookup(app: options.appId).wait()
else { else {
print("No results found")
return .failure(.noSearchResultsFound) return .failure(.noSearchResultsFound)
} }

View file

@ -79,6 +79,7 @@ extension FileHandle: TextOutputStream {
} }
} }
/// Prints a message to stdout prefixed with a blue arrow.
func printInfo(_ message: String) { func printInfo(_ message: String) {
guard isatty(fileno(stdout)) != 0 else { guard isatty(fileno(stdout)) != 0 else {
print("==> \(message)") print("==> \(message)")
@ -89,6 +90,7 @@ func printInfo(_ message: String) {
print("\(csi)1;34m==>\(csi)0m \(csi)1m\(message)\(csi)0m") 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) { public func printWarning(_ message: String) {
guard isatty(fileno(stderr)) != 0 else { guard isatty(fileno(stderr)) != 0 else {
print("Warning: \(message)", to: &standardError) print("Warning: \(message)", to: &standardError)
@ -99,6 +101,7 @@ public func printWarning(_ message: String) {
print("\(csi)4;33mWarning:\(csi)0m \(message)", to: &standardError) 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) { public func printError(_ message: String) {
guard isatty(fileno(stderr)) != 0 else { guard isatty(fileno(stderr)) != 0 else {
print("Error: \(message)", to: &standardError) print("Error: \(message)", to: &standardError)
@ -109,6 +112,7 @@ public func printError(_ message: String) {
print("\(csi)4;31mError:\(csi)0m \(message)", to: &standardError) print("\(csi)4;31mError:\(csi)0m \(message)", to: &standardError)
} }
/// Flushes stdout.
func clearLine() { func clearLine() {
guard isatty(fileno(stdout)) != 0 else { guard isatty(fileno(stdout)) != 0 else {
return return

View file

@ -19,9 +19,9 @@ public class OutdatedCommandSpec: QuickSpec {
describe("outdated command") { describe("outdated command") {
it("displays apps with pending updates") { it("displays apps with pending updates") {
let cmd = OutdatedCommand() let cmd = OutdatedCommand()
let result = cmd.run(OutdatedCommand.Options()) let result = cmd.run(OutdatedCommand.Options(verbose: true))
print(result) print(result)
// expect(result).to(beSuccess()) expect(result).to(beSuccess())
} }
} }
} }