diff options
Diffstat (limited to 'examples/servicelogs.rs')
-rw-r--r-- | examples/servicelogs.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/examples/servicelogs.rs b/examples/servicelogs.rs new file mode 100644 index 0000000..54d4743 --- /dev/null +++ b/examples/servicelogs.rs @@ -0,0 +1,32 @@ +use futures::StreamExt; +use shiplift::{tty::TtyChunk, Docker, LogsOptions}; +use std::env; + +#[tokio::main] +async fn main() { + let docker = Docker::new(); + let id = env::args() + .nth(1) + .expect("You need to specify a service name"); + + let services = docker.services(); + + let mut logs_stream = services + .get(&id) + .logs(&LogsOptions::builder().stdout(true).stderr(true).build()); + + while let Some(log_result) = logs_stream.next().await { + match log_result { + Ok(chunk) => print_chunk(chunk), + Err(e) => eprintln!("Error: {}", e), + } + } +} + +fn print_chunk(chunk: TtyChunk) { + match chunk { + TtyChunk::StdOut(bytes) => println!("Stdout: {}", std::str::from_utf8(&bytes).unwrap()), + TtyChunk::StdErr(bytes) => eprintln!("Stdout: {}", std::str::from_utf8(&bytes).unwrap()), + TtyChunk::StdIn(_) => unreachable!(), + } +} |