-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathcontainerexec.rs
37 lines (33 loc) · 1.09 KB
/
containerexec.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use futures::StreamExt;
use shiplift::{tty::TtyChunk, Docker, ExecContainerOptions};
use std::{env, str::from_utf8};
#[tokio::main]
async fn main() {
let docker = Docker::new();
let id = env::args()
.nth(1)
.expect("You need to specify a container id");
let options = ExecContainerOptions::builder()
.cmd(vec![
"bash",
"-c",
"echo -n \"echo VAR=$VAR on stdout\"; echo -n \"echo VAR=$VAR on stderr\" >&2",
])
.env(vec!["VAR=value"])
.attach_stdout(true)
.attach_stderr(true)
.build();
while let Some(exec_result) = docker.containers().get(&id).exec(&options).next().await {
match exec_result {
Ok(chunk) => print_chunk(chunk),
Err(e) => eprintln!("Error: {}", e),
}
}
}
fn print_chunk(chunk: TtyChunk) {
match chunk {
TtyChunk::StdOut(bytes) => println!("Stdout: {}", from_utf8(&bytes).unwrap()),
TtyChunk::StdErr(bytes) => eprintln!("Stdout: {}", from_utf8(&bytes).unwrap()),
TtyChunk::StdIn(_) => unreachable!(),
}
}