1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-03 17:14:08 +02:00

interactive exec

This commit is contained in:
Robin Appelman 2021-03-13 20:24:17 +01:00
commit f6b1010b55
4 changed files with 81 additions and 11 deletions

View file

@ -12,11 +12,17 @@ use futures_util::stream::StreamExt;
use min_id::generate_id;
use std::collections::HashMap;
use std::fs;
use std::io::Write;
use std::io::{stdout, Read};
use std::net::IpAddr;
use std::os::unix::fs::MetadataExt;
use std::str::FromStr;
use std::time::Duration;
use termion::async_stdin;
use termion::raw::IntoRawMode;
use tokio::fs::{create_dir_all, remove_dir_all, write};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::task::spawn;
use tokio::time::sleep;
#[derive(Default, Debug, Eq, PartialEq)]
@ -293,18 +299,45 @@ impl Cloud {
user: Some("haze".to_string()),
attach_stdout: Some(true),
attach_stderr: Some(true),
attach_stdin: Some(true),
tty: Some(true),
..Default::default()
};
let message = docker.create_exec(&self.id, config).await?;
let mut exec = docker.start_exec(&message.id, None);
while let Some(res) = exec.next().await {
match res? {
StartExecResults::Attached { log } => {
print!("{}", log);
if let StartExecResults::AttachedTTY {
mut output,
mut input,
} = docker.start_exec(&message.id, None, true).await?
{
// pipe stdin into the docker exec stream input
spawn(async move {
let mut stdin = async_stdin().bytes();
loop {
if let Some(Ok(byte)) = stdin.next() {
input.write(&[byte]).await.ok();
} else {
sleep(Duration::from_nanos(10)).await;
}
}
_ => {}
});
// set stdout in raw mode so we can do tty stuff
let stdout = stdout();
let mut stdout = stdout.lock().into_raw_mode()?;
// pipe docker exec output into stdout
let mut buff = [0; 128];
while let Ok(read) = output.read(&mut buff).await {
if read == 0 {
break;
}
stdout.write(&buff[0..read])?;
stdout.flush()?;
}
} else {
unreachable!();
}
Ok(())
}
}

View file

@ -72,7 +72,16 @@ async fn main() -> Result<()> {
}
HazeCommand::Exec => {
let cloud = get_by_filter(&mut docker, None, &config).await?;
cloud.exec(&mut docker, args.options).await?;
cloud
.exec(
&mut docker,
if args.options.is_empty() {
vec!["bash".to_string()]
} else {
args.options
},
)
.await?;
}
HazeCommand::Occ => {
let cloud = get_by_filter(&mut docker, None, &config).await?;