Skip to content

Commit 388d963

Browse files
committed
Do not return Result (or anything else) from command run(…) functions.
Throw when failure. Normal Void return when success. Signed-off-by: Ross Goldberg <[email protected]>
1 parent 3b86deb commit 388d963

34 files changed

+111
-331
lines changed

Sources/mas/Commands/Account.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,16 @@ extension Mas {
1717

1818
/// Runs the command.
1919
func run() throws {
20-
let result = runInternal()
21-
if case .failure = result {
22-
try result.get()
23-
}
24-
}
25-
26-
func runInternal() -> Result<Void, MASError> {
2720
if #available(macOS 12, *) {
2821
// Account information is no longer available as of Monterey.
2922
// https://github.com/mas-cli/mas/issues/417
30-
return .failure(.notSupported)
23+
throw MASError.notSupported
3124
}
3225

3326
do {
3427
print(try ISStoreAccount.primaryAccount.wait().identifier)
35-
return .success(())
3628
} catch {
37-
return .failure(error as? MASError ?? .failed(error: error as NSError))
29+
throw error as? MASError ?? MASError.failed(error: error as NSError)
3830
}
3931
}
4032
}

Sources/mas/Commands/Home.swift

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,29 @@ extension Mas {
2121

2222
/// Runs the command.
2323
func run() throws {
24-
let result = run(storeSearch: MasStoreSearch(), openCommand: OpenSystemCommand())
25-
if case .failure = result {
26-
try result.get()
27-
}
24+
try run(storeSearch: MasStoreSearch(), openCommand: OpenSystemCommand())
2825
}
2926

30-
func run(storeSearch: StoreSearch, openCommand: ExternalCommand) -> Result<Void, MASError> {
27+
func run(storeSearch: StoreSearch, openCommand: ExternalCommand) throws {
3128
do {
3229
guard let result = try storeSearch.lookup(app: appId).wait() else {
33-
return .failure(.noSearchResultsFound)
30+
throw MASError.noSearchResultsFound
3431
}
3532

3633
do {
3734
try openCommand.run(arguments: result.trackViewUrl)
3835
} catch {
3936
printError("Unable to launch open command")
40-
return .failure(.searchFailed)
37+
throw MASError.searchFailed
4138
}
4239
if openCommand.failed {
4340
let reason = openCommand.process.terminationReason
4441
printError("Open failed: (\(reason)) \(openCommand.stderr)")
45-
return .failure(.searchFailed)
42+
throw MASError.searchFailed
4643
}
4744
} catch {
48-
// Bubble up MASErrors
49-
if let error = error as? MASError {
50-
return .failure(error)
51-
}
52-
return .failure(.searchFailed)
45+
throw error as? MASError ?? .searchFailed
5346
}
54-
55-
return .success(())
5647
}
5748
}
5849
}

Sources/mas/Commands/Info.swift

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,19 @@ extension Mas {
2222

2323
/// Runs the command.
2424
func run() throws {
25-
let result = run(storeSearch: MasStoreSearch())
26-
if case .failure = result {
27-
try result.get()
28-
}
25+
try run(storeSearch: MasStoreSearch())
2926
}
3027

31-
func run(storeSearch: StoreSearch) -> Result<Void, MASError> {
28+
func run(storeSearch: StoreSearch) throws {
3229
do {
3330
guard let result = try storeSearch.lookup(app: appId).wait() else {
34-
return .failure(.noSearchResultsFound)
31+
throw MASError.noSearchResultsFound
3532
}
3633

3734
print(AppInfoFormatter.format(app: result))
3835
} catch {
39-
// Bubble up MASErrors
40-
if let error = error as? MASError {
41-
return .failure(error)
42-
}
43-
return .failure(.searchFailed)
36+
throw error as? MASError ?? .searchFailed
4437
}
45-
46-
return .success(())
4738
}
4839
}
4940
}

Sources/mas/Commands/Install.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@ extension Mas {
2323

2424
/// Runs the command.
2525
func run() throws {
26-
let result = run(appLibrary: MasAppLibrary())
27-
if case .failure = result {
28-
try result.get()
29-
}
26+
try run(appLibrary: MasAppLibrary())
3027
}
3128

32-
func run(appLibrary: AppLibrary) -> Result<Void, MASError> {
29+
func run(appLibrary: AppLibrary) throws {
3330
// Try to download applications with given identifiers and collect results
3431
let appIds = appIds.filter { appId in
3532
if let product = appLibrary.installedApp(forId: appId), !force {
@@ -43,10 +40,8 @@ extension Mas {
4340
do {
4441
try downloadAll(appIds).wait()
4542
} catch {
46-
return .failure(error as? MASError ?? .downloadFailed(error: error as NSError))
43+
throw error as? MASError ?? .downloadFailed(error: error as NSError)
4744
}
48-
49-
return .success(())
5045
}
5146
}
5247
}

Sources/mas/Commands/List.swift

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,16 @@ extension Mas {
1717

1818
/// Runs the command.
1919
func run() throws {
20-
let result = run(appLibrary: MasAppLibrary())
21-
if case .failure = result {
22-
try result.get()
23-
}
20+
try run(appLibrary: MasAppLibrary())
2421
}
2522

26-
func run(appLibrary: AppLibrary) -> Result<Void, MASError> {
23+
func run(appLibrary: AppLibrary) throws {
2724
let products = appLibrary.installedApps
2825
if products.isEmpty {
2926
printError("No installed apps found")
30-
return .success(())
27+
} else {
28+
print(AppListFormatter.format(products: products))
3129
}
32-
33-
let output = AppListFormatter.format(products: products)
34-
print(output)
35-
36-
return .success(())
3730
}
3831
}
3932
}

Sources/mas/Commands/Lucky.swift

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,56 +24,45 @@ extension Mas {
2424

2525
/// Runs the command.
2626
func run() throws {
27-
let result = run(appLibrary: MasAppLibrary(), storeSearch: MasStoreSearch())
28-
if case .failure = result {
29-
try result.get()
30-
}
27+
try run(appLibrary: MasAppLibrary(), storeSearch: MasStoreSearch())
3128
}
3229

33-
func run(appLibrary: AppLibrary, storeSearch: StoreSearch) -> Result<Void, MASError> {
30+
func run(appLibrary: AppLibrary, storeSearch: StoreSearch) throws {
3431
var appId: Int?
3532

3633
do {
3734
let results = try storeSearch.search(for: appName).wait()
3835
guard let result = results.first else {
3936
printError("No results found")
40-
return .failure(.noSearchResultsFound)
37+
throw MASError.noSearchResultsFound
4138
}
4239

4340
appId = result.trackId
4441
} catch {
45-
// Bubble up MASErrors
46-
if let error = error as? MASError {
47-
return .failure(error)
48-
}
49-
return .failure(.searchFailed)
42+
throw error as? MASError ?? .searchFailed
5043
}
5144

5245
guard let identifier = appId else { fatalError() }
5346

54-
return install(UInt64(identifier), appLibrary: appLibrary)
47+
try install(UInt64(identifier), appLibrary: appLibrary)
5548
}
5649

5750
/// Installs an app.
5851
///
5952
/// - Parameters:
6053
/// - appId: App identifier
6154
/// - appLibrary: Library of installed apps
62-
/// - Returns: Result of the operation.
63-
fileprivate func install(_ appId: UInt64, appLibrary: AppLibrary) -> Result<Void, MASError> {
55+
fileprivate func install(_ appId: UInt64, appLibrary: AppLibrary) throws {
6456
// Try to download applications with given identifiers and collect results
6557
if let product = appLibrary.installedApp(forId: appId), !force {
6658
printWarning("\(product.appName) is already installed")
67-
return .success(())
68-
}
69-
70-
do {
71-
try downloadAll([appId]).wait()
72-
} catch {
73-
return .failure(error as? MASError ?? .downloadFailed(error: error as NSError))
59+
} else {
60+
do {
61+
try downloadAll([appId]).wait()
62+
} catch {
63+
throw error as? MASError ?? .downloadFailed(error: error as NSError)
64+
}
7465
}
75-
76-
return .success(())
7766
}
7867
}
7968
}

Sources/mas/Commands/Open.swift

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,57 +25,48 @@ extension Mas {
2525

2626
/// Runs the command.
2727
func run() throws {
28-
let result = run(storeSearch: MasStoreSearch(), openCommand: OpenSystemCommand())
29-
if case .failure = result {
30-
try result.get()
31-
}
28+
try run(storeSearch: MasStoreSearch(), openCommand: OpenSystemCommand())
3229
}
3330

34-
func run(storeSearch: StoreSearch, openCommand: ExternalCommand) -> Result<Void, MASError> {
31+
func run(storeSearch: StoreSearch, openCommand: ExternalCommand) throws {
3532
do {
3633
if appId == markerValue {
3734
// If no app ID is given, just open the MAS GUI app
3835
try openCommand.run(arguments: masScheme + "://")
39-
return .success(())
36+
return
4037
}
4138

4239
guard let appId = Int(appId)
4340
else {
4441
printError("Invalid app ID")
45-
return .failure(.noSearchResultsFound)
42+
throw MASError.noSearchResultsFound
4643
}
4744

4845
guard let result = try storeSearch.lookup(app: appId).wait()
4946
else {
50-
return .failure(.noSearchResultsFound)
47+
throw MASError.noSearchResultsFound
5148
}
5249

5350
guard var url = URLComponents(string: result.trackViewUrl)
5451
else {
55-
return .failure(.searchFailed)
52+
throw MASError.searchFailed
5653
}
5754
url.scheme = masScheme
5855

5956
do {
6057
try openCommand.run(arguments: url.string!)
6158
} catch {
6259
printError("Unable to launch open command")
63-
return .failure(.searchFailed)
60+
throw MASError.searchFailed
6461
}
6562
if openCommand.failed {
6663
let reason = openCommand.process.terminationReason
6764
printError("Open failed: (\(reason)) \(openCommand.stderr)")
68-
return .failure(.searchFailed)
65+
throw MASError.searchFailed
6966
}
7067
} catch {
71-
// Bubble up MASErrors
72-
if let error = error as? MASError {
73-
return .failure(error)
74-
}
75-
return .failure(.searchFailed)
68+
throw error as? MASError ?? .searchFailed
7669
}
77-
78-
return .success(())
7970
}
8071
}
8172
}

Sources/mas/Commands/Outdated.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@ extension Mas {
2525

2626
/// Runs the command.
2727
func run() throws {
28-
let result = run(appLibrary: MasAppLibrary(), storeSearch: MasStoreSearch())
29-
if case .failure = result {
30-
try result.get()
31-
}
28+
try run(appLibrary: MasAppLibrary(), storeSearch: MasStoreSearch())
3229
}
3330

34-
func run(appLibrary: AppLibrary, storeSearch: StoreSearch) -> Result<Void, MASError> {
31+
func run(appLibrary: AppLibrary, storeSearch: StoreSearch) throws {
3532
let promises = appLibrary.installedApps.map { installedApp in
3633
firstly {
3734
storeSearch.lookup(app: installedApp.itemIdentifier.intValue)
@@ -59,12 +56,11 @@ extension Mas {
5956
}
6057
}
6158

62-
return firstly {
59+
_ = firstly {
6360
when(fulfilled: promises)
6461
}.map {
6562
Result<Void, MASError>.success(())
6663
}.recover { error in
67-
// Bubble up MASErrors
6864
.value(Result<Void, MASError>.failure(error as? MASError ?? .searchFailed))
6965
}.wait()
7066
}

Sources/mas/Commands/Purchase.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ extension Mas {
2020

2121
/// Runs the command.
2222
func run() throws {
23-
let result = run(appLibrary: MasAppLibrary())
24-
if case .failure = result {
25-
try result.get()
26-
}
23+
try run(appLibrary: MasAppLibrary())
2724
}
2825

29-
func run(appLibrary: AppLibrary) -> Result<Void, MASError> {
26+
func run(appLibrary: AppLibrary) throws {
3027
// Try to download applications with given identifiers and collect results
3128
let appIds = appIds.filter { appId in
3229
if let product = appLibrary.installedApp(forId: appId) {
@@ -40,10 +37,8 @@ extension Mas {
4037
do {
4138
try downloadAll(appIds, purchase: true).wait()
4239
} catch {
43-
return .failure(error as? MASError ?? .downloadFailed(error: error as NSError))
40+
throw error as? MASError ?? .downloadFailed(error: error as NSError)
4441
}
45-
46-
return .success(())
4742
}
4843
}
4944
}

Sources/mas/Commands/Reset.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ extension Mas {
2121

2222
/// Runs the command.
2323
func run() throws {
24-
let result = runInternal()
25-
if case .failure = result {
26-
try result.get()
27-
}
28-
}
29-
30-
func runInternal() -> Result<Void, MASError> {
3124
// The "Reset Application" command in the Mac App Store debug menu performs
3225
// the following steps
3326
//
@@ -81,8 +74,6 @@ extension Mas {
8174
}
8275
}
8376
}
84-
85-
return .success(())
8677
}
8778
}
8879
}

0 commit comments

Comments
 (0)