Skip to content

Commit 0a3c929

Browse files
committed
Refactored JSON Output for Install Command
1 parent 36f02ca commit 0a3c929

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

Sources/Swiftly/Install.swift

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,17 @@ struct Install: SwiftlyCommand {
8181
))
8282
var progressFile: FilePath?
8383

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

8689
private enum CodingKeys: String, CodingKey {
87-
case version, use, verify, postInstallFile, root, progressFile
90+
case version, use, verify, postInstallFile, root, progressFile, format
8891
}
8992

9093
mutating func run() async throws {
91-
try await self.run(Swiftly.createDefaultContext())
94+
try await self.run(Swiftly.createDefaultContext(format: self.format))
9295
}
9396

9497
private func swiftlyHomeDir(_ ctx: SwiftlyCoreContext) -> FilePath {
@@ -266,7 +269,10 @@ struct Install: SwiftlyCommand {
266269
progressFile: FilePath? = nil
267270
) async throws -> (postInstall: String?, pathChanged: Bool) {
268271
guard !config.installedToolchains.contains(version) else {
269-
await ctx.message("\(version) is already installed.")
272+
let installInfo = InstallInfo(
273+
version: version, alreadyInstalled: true
274+
)
275+
try await ctx.output(installInfo)
270276
return (nil, false)
271277
}
272278

@@ -316,6 +322,8 @@ struct Install: SwiftlyCommand {
316322
if let progressFile
317323
{
318324
try JsonFileProgressReporter(ctx, filePath: progressFile)
325+
} else if ctx.format == .json {
326+
nil
319327
} else {
320328
ConsoleProgressReporter(stream: stdoutStream, header: "Downloading \(version)")
321329
}
@@ -351,7 +359,7 @@ struct Install: SwiftlyCommand {
351359
lastUpdate = Date()
352360

353361
do {
354-
try await animation.update(
362+
try await animation?.update(
355363
step: progress.receivedBytes,
356364
total: progress.totalBytes!,
357365
text:
@@ -368,10 +376,10 @@ struct Install: SwiftlyCommand {
368376
throw SwiftlyError(
369377
message: "\(version) does not exist at URL \(notFound.url), exiting")
370378
} catch {
371-
try? await animation.complete(success: false)
379+
try? await animation?.complete(success: false)
372380
throw error
373381
}
374-
try await animation.complete(success: true)
382+
try await animation?.complete(success: true)
375383

376384
if verifySignature {
377385
try await Swiftly.currentPlatform.verifyToolchainSignature(
@@ -427,7 +435,11 @@ struct Install: SwiftlyCommand {
427435
return (pathChanged, config)
428436
}
429437
config = newConfig
430-
await ctx.message("\(version) installed successfully!")
438+
let installInfo = InstallInfo(
439+
version: version,
440+
alreadyInstalled: false
441+
)
442+
try await ctx.output(installInfo)
431443
return (postInstallScript, pathChanged)
432444
}
433445
}

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)