2024-03-15 15:59:18 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use clap::Parser;
|
|
|
|
use krata::control::control_service_client::ControlServiceClient;
|
|
|
|
|
2024-03-15 17:36:26 +00:00
|
|
|
use tokio::select;
|
2024-03-15 15:59:18 +00:00
|
|
|
use tonic::transport::Channel;
|
|
|
|
|
|
|
|
use crate::{console::StdioConsoleStream, events::EventStream};
|
|
|
|
|
|
|
|
#[derive(Parser)]
|
|
|
|
pub struct ConsoleCommand {
|
|
|
|
#[arg()]
|
|
|
|
guest: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConsoleCommand {
|
|
|
|
pub async fn run(
|
|
|
|
self,
|
|
|
|
mut client: ControlServiceClient<Channel>,
|
|
|
|
events: EventStream,
|
|
|
|
) -> Result<()> {
|
|
|
|
let input = StdioConsoleStream::stdin_stream(self.guest.clone()).await;
|
|
|
|
let output = client.console_data(input).await?.into_inner();
|
2024-03-15 17:36:26 +00:00
|
|
|
let stdout_handle =
|
|
|
|
tokio::task::spawn(async move { StdioConsoleStream::stdout(output).await });
|
2024-03-15 15:59:18 +00:00
|
|
|
let exit_hook_task =
|
|
|
|
StdioConsoleStream::guest_exit_hook(self.guest.clone(), events).await?;
|
2024-03-15 17:36:26 +00:00
|
|
|
let code = select! {
|
|
|
|
x = stdout_handle => {
|
|
|
|
x??;
|
|
|
|
None
|
|
|
|
},
|
|
|
|
x = exit_hook_task => x?
|
|
|
|
};
|
|
|
|
std::process::exit(code.unwrap_or(0));
|
2024-03-15 15:59:18 +00:00
|
|
|
}
|
|
|
|
}
|