-
Notifications
You must be signed in to change notification settings - Fork 55
json output install #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
json output install #392
Changes from 5 commits
79f5c8b
3e9e091
36f02ca
6d83aef
176e123
5d0f49f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,14 +81,17 @@ struct Install: SwiftlyCommand { | |
)) | ||
var progressFile: FilePath? | ||
|
||
@Option(name: .long, help: "Output format (text, json)") | ||
var format: SwiftlyCore.OutputFormat = .text | ||
|
||
@OptionGroup var root: GlobalOptions | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
case version, use, verify, postInstallFile, root, progressFile | ||
case version, use, verify, postInstallFile, root, progressFile, format | ||
} | ||
|
||
mutating func run() async throws { | ||
try await self.run(Swiftly.createDefaultContext()) | ||
try await self.run(Swiftly.createDefaultContext(format: self.format)) | ||
} | ||
|
||
private func swiftlyHomeDir(_ ctx: SwiftlyCoreContext) -> FilePath { | ||
|
@@ -266,7 +269,10 @@ struct Install: SwiftlyCommand { | |
progressFile: FilePath? = nil | ||
) async throws -> (postInstall: String?, pathChanged: Bool) { | ||
guard !config.installedToolchains.contains(version) else { | ||
await ctx.message("\(version) is already installed.") | ||
let installInfo = InstallInfo( | ||
version: version, alreadyInstalled: true | ||
) | ||
try await ctx.output(installInfo) | ||
return (nil, false) | ||
} | ||
|
||
|
@@ -312,16 +318,18 @@ struct Install: SwiftlyCommand { | |
} | ||
} | ||
|
||
let animation: ProgressReporterProtocol = | ||
let animation: ProgressReporterProtocol? = | ||
if let progressFile | ||
{ | ||
try JsonFileProgressReporter(ctx, filePath: progressFile) | ||
} else if ctx.format == .json { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Since we've been generally putting messages on stderr when json formatted output is requested, could the ConsoleProgressReporter do the same thing? |
||
nil | ||
} else { | ||
ConsoleProgressReporter(stream: stdoutStream, header: "Downloading \(version)") | ||
ConsoleProgressReporter(stream: stderrStream, header: "Downloading \(version)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (blocking): The default case should continue to report progress to stdout. The json output case would be the one to output to stderr, so that the progress wouldn't corrupt the json. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohh, yes I missed this while earlier review. |
||
} | ||
|
||
defer { | ||
try? animation.close() | ||
try? animation?.close() | ||
} | ||
|
||
var lastUpdate = Date() | ||
|
@@ -351,7 +359,7 @@ struct Install: SwiftlyCommand { | |
lastUpdate = Date() | ||
|
||
do { | ||
try await animation.update( | ||
try await animation?.update( | ||
step: progress.receivedBytes, | ||
total: progress.totalBytes!, | ||
text: | ||
|
@@ -368,10 +376,10 @@ struct Install: SwiftlyCommand { | |
throw SwiftlyError( | ||
message: "\(version) does not exist at URL \(notFound.url), exiting") | ||
} catch { | ||
try? await animation.complete(success: false) | ||
try? await animation?.complete(success: false) | ||
throw error | ||
} | ||
try await animation.complete(success: true) | ||
try await animation?.complete(success: true) | ||
|
||
if verifySignature { | ||
try await Swiftly.currentPlatform.verifyToolchainSignature( | ||
|
@@ -427,7 +435,11 @@ struct Install: SwiftlyCommand { | |
return (pathChanged, config) | ||
} | ||
config = newConfig | ||
await ctx.message("\(version) installed successfully!") | ||
let installInfo = InstallInfo( | ||
version: version, | ||
alreadyInstalled: false | ||
) | ||
try await ctx.output(installInfo) | ||
return (postInstallScript, pathChanged) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Now that the animation will always get a non-nil value in each case, this variable can become non-optional type. That should make the code below a bit simpler since it doesn't have to handle the optionality in every case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a condition that requires animation to be optional for the JSON format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see now. Can the
ctx.format == .json
case create a ConsoleProgressReporter with stderr as the stream, and then the else case uses stdout as the stream?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that is doable.