Skip to content

Commit 66d38b0

Browse files
committed
Refactored JSON Output for Install Command
1 parent 8d9fbcb commit 66d38b0

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

Sources/Swiftly/Install.swift

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,17 @@ struct Install: SwiftlyCommand {
8282
))
8383
var progressFile: FilePath?
8484

85+
@Option(name: .long, help: "Output format (text, json)")
86+
var format: SwiftlyCore.OutputFormat = .text
87+
8588
@OptionGroup var root: GlobalOptions
8689

8790
private enum CodingKeys: String, CodingKey {
88-
case version, use, verify, postInstallFile, root, progressFile
91+
case version, use, verify, postInstallFile, root, progressFile, format
8992
}
9093

9194
mutating func run() async throws {
92-
try await self.run(Swiftly.createDefaultContext())
95+
try await self.run(Swiftly.createDefaultContext(format: self.format))
9396
}
9497

9598
private func swiftlyHomeDir(_ ctx: SwiftlyCoreContext) -> FilePath {
@@ -267,7 +270,10 @@ struct Install: SwiftlyCommand {
267270
progressFile: FilePath? = nil
268271
) async throws -> (postInstall: String?, pathChanged: Bool) {
269272
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)
271277
return (nil, false)
272278
}
273279

@@ -313,10 +319,12 @@ struct Install: SwiftlyCommand {
313319
}
314320
}
315321

316-
let animation: ProgressAnimationProtocol =
322+
let animation: ProgressAnimationProtocol? =
317323
if let progressFile
318324
{
319325
try JsonFileProgressReporter(ctx, filePath: progressFile)
326+
} else if ctx.format == .json {
327+
nil
320328
} else {
321329
PercentProgressAnimation(stream: stdoutStream, header: "Downloading \(version)")
322330
}
@@ -351,7 +359,7 @@ struct Install: SwiftlyCommand {
351359

352360
lastUpdate = Date()
353361

354-
animation.update(
362+
animation?.update(
355363
step: progress.receivedBytes,
356364
total: progress.totalBytes!,
357365
text:
@@ -363,10 +371,10 @@ struct Install: SwiftlyCommand {
363371
throw SwiftlyError(
364372
message: "\(version) does not exist at URL \(notFound.url), exiting")
365373
} catch {
366-
animation.complete(success: false)
374+
animation?.complete(success: false)
367375
throw error
368376
}
369-
animation.complete(success: true)
377+
animation?.complete(success: true)
370378

371379
if verifySignature {
372380
try await Swiftly.currentPlatform.verifyToolchainSignature(
@@ -422,7 +430,11 @@ struct Install: SwiftlyCommand {
422430
return (pathChanged, config)
423431
}
424432
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)
426438
return (postInstallScript, pathChanged)
427439
}
428440
}

Sources/Swiftly/OutputSchema.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,28 @@ struct InstalledToolchainsListInfo: OutputData {
339339
return lines.joined(separator: "\n")
340340
}
341341
}
342+
343+
struct InstallInfo: OutputData {
344+
let version: ToolchainVersion
345+
let alreadyInstalled: Bool
346+
347+
init(version: ToolchainVersion, alreadyInstalled: Bool) {
348+
self.version = version
349+
self.alreadyInstalled = alreadyInstalled
350+
}
351+
352+
var description: String {
353+
"\(self.version) is \(self.alreadyInstalled ? "already installed" : "installed successfully!")"
354+
}
355+
356+
private enum CodingKeys: String, CodingKey {
357+
case version
358+
case alreadyInstalled
359+
}
360+
361+
public func encode(to encoder: Encoder) throws {
362+
var container = encoder.container(keyedBy: CodingKeys.self)
363+
try container.encode(self.version.name, forKey: .version)
364+
try container.encode(self.alreadyInstalled, forKey: .alreadyInstalled)
365+
}
366+
}

0 commit comments

Comments
 (0)