@@ -34,6 +34,9 @@ struct Download: AsyncParsableCommand {
34
34
@Option ( name: [ . long] , help: " The log level. " )
35
35
private var logLevel : LogLevel = . info
36
36
37
+ @Flag ( name: . long, help: " Obtain a license for the app if needed. " )
38
+ private var purchase : Bool = false
39
+
37
40
lazy var logger = ConsoleLogger ( level: logLevel)
38
41
}
39
42
@@ -66,7 +69,47 @@ extension Download {
66
69
}
67
70
}
68
71
69
- private mutating func item( from app: iTunesResponse . Result , account: Account ) async -> StoreResponse . Item {
72
+
73
+ private mutating func purchase( app: iTunesResponse . Result , account: Account ) async {
74
+ logger. log ( " Creating HTTP client... " , level: . debug)
75
+ let httpClient = HTTPClient ( session: URLSession . shared)
76
+
77
+ logger. log ( " Creating App Store client... " , level: . debug)
78
+ let storeClient = StoreClient ( httpClient: httpClient)
79
+
80
+ do {
81
+ logger. log ( " Obtaining a license for ' \( app. identifier) ' from the App Store... " , level: . info)
82
+ try await storeClient. purchase (
83
+ identifier: " \( app. identifier) " ,
84
+ directoryServicesIdentifier: account. directoryServicesIdentifier,
85
+ passwordToken: account. passwordToken,
86
+ countryCode: countryCode
87
+ )
88
+ } catch {
89
+ logger. log ( " \( error) " , level: . debug)
90
+
91
+ switch error {
92
+ case StoreClient . Error. purchaseFailed:
93
+ logger. log ( " Purchase failed. " , level: . error)
94
+ case StoreClient . Error. duplicateLicense:
95
+ logger. log ( " A license already exists for this item. " , level: . error)
96
+ case StoreResponse . Error. invalidCountry:
97
+ logger. log ( " The country provided does not match with the account you are using. Supply a valid country using the \" --country \" flag. " , level: . error)
98
+ case StoreResponse . Error. passwordTokenExpired:
99
+ logger. log ( " Token expired. Login again using the \" auth \" command. " , level: . error)
100
+ default :
101
+ logger. log ( " An unknown error has occurred. " , level: . error)
102
+ }
103
+
104
+ _exit ( 1 )
105
+ }
106
+ }
107
+
108
+ private mutating func item(
109
+ from app: iTunesResponse . Result ,
110
+ account: Account ,
111
+ purchaseAttempted: Bool = false
112
+ ) async -> StoreResponse . Item {
70
113
logger. log ( " Creating HTTP client... " , level: . debug)
71
114
let httpClient = HTTPClient ( session: URLSession . shared)
72
115
@@ -88,7 +131,16 @@ extension Download {
88
131
case StoreResponse . Error. invalidItem:
89
132
logger. log ( " Received invalid store item. " , level: . error)
90
133
case StoreResponse . Error. invalidLicense:
91
- logger. log ( " Your Apple ID does not have a license for this app. Use the \" purchase \" command to obtain a license. " , level: . error)
134
+ if !purchaseAttempted, purchase {
135
+ logger. log ( " License is missing. " , level: . info)
136
+
137
+ await purchase ( app: app, account: account)
138
+ logger. log ( " Obtained a license for ' \( app. identifier) '. " , level: . debug)
139
+
140
+ return await item ( from: app, account: account, purchaseAttempted: true )
141
+ } else {
142
+ logger. log ( " Your Apple ID does not have a license for this app. Use the \" purchase \" command or the \" --purchase \" to obtain a license. " , level: . error)
143
+ }
92
144
case StoreResponse . Error. invalidCountry:
93
145
logger. log ( " The country provided does not match with the account you are using. Supply a valid country using the \" --country \" flag. " , level: . error)
94
146
case StoreResponse . Error. passwordTokenExpired:
0 commit comments