Skip to content

Commit

Permalink
Change to raw TTY access
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed Sep 19, 2024
1 parent 89f570c commit 1885fb4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
25 changes: 12 additions & 13 deletions Sources/Citadel/TTY/Client/TTY.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ extension SSHClient {
}

internal func _executeCommandStream(
_ command: String,
_ command: String?,
inShell: Bool = false
) async throws -> (channel: Channel, output: AsyncThrowingStream<ExecCommandOutput, Error>) {
let (stream, streamContinuation) = AsyncThrowingStream<ExecCommandOutput, Error>.makeStream()
Expand All @@ -227,13 +227,14 @@ extension SSHClient {
streamContinuation.finish()
}
case .channelSuccess:
if inShell,
!hasReceivedChannelSuccess {
let commandData = SSHChannelData(
type: .channel,
data: .byteBuffer(ByteBuffer(string: command + ";exit\n"))
)
channel.writeAndFlush(commandData, promise: nil)
if inShell, !hasReceivedChannelSuccess {
if let command {
let commandData = SSHChannelData(
type: .channel,
data: .byteBuffer(ByteBuffer(string: command + ";exit\n"))
)
channel.writeAndFlush(commandData, promise: nil)
}
hasReceivedChannelSuccess = true
}
case .exit(let status):
Expand All @@ -258,7 +259,7 @@ extension SSHClient {
try await channel.triggerUserOutboundEvent(SSHChannelRequestEvent.ShellRequest(
wantReply: true
))
} else {
} else if let command {
try await channel.triggerUserOutboundEvent(SSHChannelRequestEvent.ExecRequest(
command: command,
wantReply: true
Expand All @@ -269,12 +270,10 @@ extension SSHClient {
}

@available(macOS 15.0, *)
public func withExecutingCommand(
_ command: String,
inShell: Bool,
public func withTTY(
perform: (_ inbound: TTYOutput, _ outbound: TTYStdinWriter) async throws -> Void
) async throws {
let (channel, output) = try await _executeCommandStream(command, inShell: inShell)
let (channel, output) = try await _executeCommandStream(nil, inShell: true)

func close() async throws {
try await channel.close()
Expand Down
3 changes: 2 additions & 1 deletion Tests/CitadelTests/Citadel2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ final class Citadel2Tests: XCTestCase {
reconnect: .never
)

try await client.withExecutingCommand("cat", inShell: false) { inbound, outbound in
try await client.withTTY { inbound, outbound in
try await outbound.write(ByteBuffer(string: "cat"))
try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
var i = UInt8.min
Expand Down

0 comments on commit 1885fb4

Please sign in to comment.