@@ -82,14 +82,17 @@ struct Install: SwiftlyCommand {
82
82
) )
83
83
var progressFile : FilePath ?
84
84
85
+ @Option ( name: . long, help: " Output format (text, json) " )
86
+ var format : SwiftlyCore . OutputFormat = . text
87
+
85
88
@OptionGroup var root : GlobalOptions
86
89
87
90
private enum CodingKeys : String , CodingKey {
88
- case version, use, verify, postInstallFile, root, progressFile
91
+ case version, use, verify, postInstallFile, root, progressFile, format
89
92
}
90
93
91
94
mutating func run( ) async throws {
92
- try await self . run ( Swiftly . createDefaultContext ( ) )
95
+ try await self . run ( Swiftly . createDefaultContext ( format : self . format ) )
93
96
}
94
97
95
98
private func swiftlyHomeDir( _ ctx: SwiftlyCoreContext ) -> FilePath {
@@ -267,7 +270,10 @@ struct Install: SwiftlyCommand {
267
270
progressFile: FilePath? = nil
268
271
) async throws -> ( postInstall: String? , pathChanged: Bool) {
269
272
guard !config. installedToolchains. contains ( version) else {
270
- await ctx. message ( " \( version) is already installed. " )
273
+ let installInfo = InstallInfo (
274
+ version: version, alreadyInstalled: true
275
+ )
276
+ try await ctx. output ( installInfo)
271
277
return ( nil , false )
272
278
}
273
279
@@ -313,10 +319,12 @@ struct Install: SwiftlyCommand {
313
319
}
314
320
}
315
321
316
- let animation : ProgressAnimationProtocol =
322
+ let animation : ProgressAnimationProtocol ? =
317
323
if let progressFile
318
324
{
319
325
try JsonFileProgressReporter ( ctx, filePath: progressFile)
326
+ } else if ctx. format == . json {
327
+ nil
320
328
} else {
321
329
PercentProgressAnimation ( stream: stdoutStream, header: " Downloading \( version) " )
322
330
}
@@ -351,7 +359,7 @@ struct Install: SwiftlyCommand {
351
359
352
360
lastUpdate = Date ( )
353
361
354
- animation. update (
362
+ animation? . update (
355
363
step: progress. receivedBytes,
356
364
total: progress. totalBytes!,
357
365
text:
@@ -363,10 +371,10 @@ struct Install: SwiftlyCommand {
363
371
throw SwiftlyError (
364
372
message: " \( version) does not exist at URL \( notFound. url) , exiting " )
365
373
} catch {
366
- animation. complete ( success: false )
374
+ animation? . complete ( success: false )
367
375
throw error
368
376
}
369
- animation. complete ( success: true )
377
+ animation? . complete ( success: true )
370
378
371
379
if verifySignature {
372
380
try await Swiftly . currentPlatform. verifyToolchainSignature (
@@ -422,7 +430,11 @@ struct Install: SwiftlyCommand {
422
430
return ( pathChanged, config)
423
431
}
424
432
config = newConfig
425
- await ctx. message ( " \( version) installed successfully! " )
433
+ let installInfo = InstallInfo (
434
+ version: version,
435
+ alreadyInstalled: false
436
+ )
437
+ try await ctx. output ( installInfo)
426
438
return ( postInstallScript, pathChanged)
427
439
}
428
440
}
0 commit comments