feat: exec tty support

This commit is contained in:
Alex Zenla
2024-04-22 23:02:14 +00:00
parent 284ed8f17b
commit 2c9152d433
10 changed files with 286 additions and 106 deletions

View File

@ -21,6 +21,8 @@ pub struct ExecCommand {
env: Option<Vec<String>>,
#[arg(short = 'w', long, help = "Working directory")]
working_directory: Option<String>,
#[arg(short = 't', long, help = "Allocate tty")]
tty: bool,
#[arg(help = "Guest to exec inside, either the name or the uuid")]
guest: String,
#[arg(
@ -47,14 +49,16 @@ impl ExecCommand {
command: self.command,
working_directory: self.working_directory.unwrap_or_default(),
}),
data: vec![],
tty: self.tty,
stdin: vec![],
stdin_closed: false,
};
let stream = StdioConsoleStream::stdin_stream_exec(initial).await;
let response = client.exec_guest(Request::new(stream)).await?.into_inner();
let code = StdioConsoleStream::exec_output(response).await?;
let result = StdioConsoleStream::exec_output(self.tty, response).await;
StdioConsoleStream::restore_terminal_mode();
let code = result?;
std::process::exit(code);
}
}

View File

@ -68,7 +68,13 @@ impl StdioConsoleStream {
if size == 1 && buffer[0] == 0x1d {
break;
}
yield ExecGuestRequest { guest_id: String::default(), task: None, data };
let closed = size == 0;
yield ExecGuestRequest { guest_id: String::default(), task: None, tty: false, stdin: data, stdin_closed: closed };
if closed {
break;
}
}
}
}
@ -90,7 +96,11 @@ impl StdioConsoleStream {
Ok(())
}
pub async fn exec_output(mut stream: Streaming<ExecGuestReply>) -> Result<i32> {
pub async fn exec_output(tty: bool, mut stream: Streaming<ExecGuestReply>) -> Result<i32> {
if tty && stdin().is_tty() {
enable_raw_mode()?;
StdioConsoleStream::register_terminal_restore_hook()?;
}
let mut stdout = stdout();
let mut stderr = stderr();
while let Some(reply) = stream.next().await {