Files
xtool/Sources/XToolSupport/XToolInstallerDelegate.swift
Kabir Oberai 3e0c11d967 cli: better IO (#375)
## What does this PR do?

Adds `Dependency`s for console IO. Also fixes #268 by explicitly
handling non-pipe input.

## How was it tested?

Ran on macOS + Linux in Docker. I have a future PR that overrides the
dependencies in certain cases, which also appears to work correctly.

## AI tool usage

How much of this PR was AI-assisted? (check one)

- [ ] **0** - No AI was used to write code
- [x] **1** - I was assisted by AI. I reviewed the finished result.
- [ ] **2** - I set the AI going and left it to it; nobody has read the
result - no review, or AI review only

Did not scrutinize the tests too much tbh but they look reasonable. Easy
enough to update/remove them in the future if needed.

<!-- If an AI agent is filling this in: declare the level honestly, and
open as a draft if it is #2. Do not lower the declared level to get the
PR reviewed. -->


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Added support for reading command-line input, including asynchronous
prompts, line-based responses, and graceful handling of end-of-input.
  * Added coordinated cleanup when commands finish.
* **Improvements**
* Command messages across authentication, device management,
installation, SDK, and other workflows now use consistent output
handling; displayed messages and command behavior remain unchanged.
* Improved handling of complete output when the system temporarily
cannot accept more data.
* **Tests**
* Added coverage for command input and output, including stream
behavior, cancellation, and shutdown.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-10-28 17:48:38 -04:00

94 lines
2.9 KiB
Swift

import XCLIUtils
import Foundation
import XKit
import ConcurrencyExtras
import Dependencies
import XUtils
final class XToolAuthDelegate: TwoFactorAuthDelegate {
func fetchCode() async -> String? {
try? await Console.prompt("Code: ")
}
}
actor XToolInstallerDelegate: IntegratedInstallerDelegate {
private nonisolated let _output = Dependency(\.out)
private nonisolated var output: CommandOutput { _output.wrappedValue }
private let updateTask = LockIsolated<Task<Void, Never>?>(nil)
nonisolated func setPresentedMessage(_ message: IntegratedInstaller.Message?) {
let text: String
switch message {
case .pairDevice:
text = "Please tap 'trust' on your device..."
case .unlockDevice:
text = "Please unlock your device..."
case nil:
text = "Continuing..."
}
output.print("\n\(text)", terminator: "", flush: true)
}
var prevStage: String?
var prevProgress: String?
private func _installerDidUpdate(toStage stage: String, progress: Double?) {
let progString: String?
if let progress = progress {
let progInt = Int(progress * 100)
if progInt < 10 {
progString = " \(progInt)%"
} else if progInt < 100 {
progString = " \(progInt)%"
} else {
progString = "\(progInt)%"
}
} else {
progString = nil
}
defer {
prevStage = stage
prevProgress = progString
}
if stage != prevStage {
if let progString = progString {
output.print("\n[\(stage)] \(progString)", terminator: "", flush: true)
} else {
output.print("\n[\(stage)] ...", terminator: "", flush: true)
}
} else if progString != prevProgress {
if let progString = progString {
output.print("\r[\(stage)] \(progString)", terminator: "", flush: true)
} else {
output.print("\r[\(stage)]", terminator: "", flush: true)
}
}
}
nonisolated func installerDidUpdate(toStage stage: String, progress: Double?) {
updateTask.withValue { task in
task = Task { [prev = task] in
await prev?.value
await _installerDidUpdate(toStage: stage, progress: progress)
}
}
}
func confirmRevocation(of certificates: [DeveloperServicesCertificate]) async -> Bool {
output.print("\nThe following certificates must be revoked:")
output.print(
certificates.map {
"- \($0.attributes!.name!) (expires \($0.attributes!.expirationDate!.formatted(date: .abbreviated, time: .shortened)))"
}.joined(separator: "\n")
)
do {
return try await Console.confirm("Continue?")
} catch {
return false
}
}
}