From 6cd1d7f93bd6f150341582a1b54087cefffdbf87 Mon Sep 17 00:00:00 2001 From: "Eli W. Hunter" <42009212+elihunter173@users.noreply.github.com> Date: Thu, 23 Jul 2020 23:54:12 -0400 Subject: Async/Await Support (continuation of #191) (#229) * it builds! * remove unused dependencies * bump dependencies * reimplement 'exec' endpoint * update a few more examples * update remaining examples * fix doc tests, remove unused 'read' module * remove feature-gated async closures * split futures dependency to just 'futures-util' * update version and readme * make functions accepting Body generic over Into again * update changelog * reinstate 'unix-socket' feature * reinstate 'attach' endpoint * fix clippy lints * fix documentation typo * fix container copyfrom/into implementations * add convenience methods for TtyChunk struct * remove 'main' from code example to silence clippy lint * Update hyper to 0.13.1 * Add Send bounds to TtyWriter * Appease clippy * Fix examples * Update issue in changelog Co-authored-by: Daniel Eades Co-authored-by: Marc Schreiber --- examples/logs.rs | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'examples/logs.rs') diff --git a/examples/logs.rs b/examples/logs.rs index 57f12be..73c8045 100644 --- a/examples/logs.rs +++ b/examples/logs.rs @@ -1,25 +1,31 @@ -use shiplift::{tty::StreamType, Docker, LogsOptions}; +use futures::StreamExt; +use shiplift::{tty::TtyChunk, Docker, LogsOptions}; use std::env; -use tokio::prelude::{Future, Stream}; -fn main() { +#[tokio::main] +async fn main() { let docker = Docker::new(); let id = env::args() .nth(1) .expect("You need to specify a container id"); - let fut = docker + + let mut logs_stream = docker .containers() .get(&id) - .logs(&LogsOptions::builder().stdout(true).stderr(true).build()) - .for_each(|chunk| { - match chunk.stream_type { - StreamType::StdOut => println!("Stdout: {}", chunk.as_string_lossy()), - StreamType::StdErr => eprintln!("Stderr: {}", chunk.as_string_lossy()), - StreamType::StdIn => unreachable!(), - } - Ok(()) - }) - .map_err(|e| eprintln!("Error: {}", e)); + .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), + } + } +} - tokio::run(fut); +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!(), + } } -- cgit v1.2.3