From 79d65c286025c551a775c0964d168e6feb4b3409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20B=C3=BCsch?= Date: Wed, 14 Nov 2018 20:36:14 +1100 Subject: Async api (#128) * Refactored Transport for better async use Still a bit rough, but it now builds a big future using combinators. It still does one `Runtime::block_on()` to keep the existing API, but this is a first up before making the whole API async. * Migrate most APIs to be Future-based I still need to finish a few of the more tricky ones that I've commented out for now, but most of it compiles and some examples work. In particular, `Docker::stats()` now properly returns an async stream of stats. * Fix events and containerinspect examples * Fix imageinspect, images, info and top examples * Fix containercreate, imagedelete and imagepull examples * Fix more examples * Add back debug statement in Transport::request * De-glob imports in examples * Remove unused imports in examples * Fix NetworkCreateOptions serialization * Add back error message extraction in Transport * Fix Container::create serialization of options * Add containerdelete example * Simplify result * Fix some error handling to remove unwrap() * Fix Image::export() * Fix imagebuild example * Add adapter from Stream of Chunks to AsyncRead Having an `AsyncRead` is required to be able to use the `FramedRead` and `Decoder` stuff from tokio_codec. This code is "borrowed" from https:/github.com/ferristseng/rust-ipfs-api though should probably be moved to its own crate or to tokio_codec. * Fix Container::logs() It now properly demuxes stdout/stderr, and returns a `Stream`. * Fix Container::export() * Use LineCodec for streaming JSON Although in my limited testing it seemed to work fine, there is no guarantee that 1 chunk == 1 piece of valid JSON. However, each JSON structure seems to be serialized on one line, so use LineCodec to turn the body into a stream of lines, then deserialize over this. * Fix serialization of ExecContainerOptions * Fix Container::exec() (kind of...) * Simplify deserialisation in Image::delete() * Small clean-ups * More clean ups * Fix rustdoc + remove extraneous "extern crate" * Fix doc example * Fix formatting --- examples/containerexec.rs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'examples/containerexec.rs') diff --git a/examples/containerexec.rs b/examples/containerexec.rs index e10d245..0b3575b 100644 --- a/examples/containerexec.rs +++ b/examples/containerexec.rs @@ -1,10 +1,16 @@ extern crate shiplift; +extern crate tokio; -use shiplift::{Docker, ExecContainerOptions}; +use shiplift::{tty::StreamType, Docker, ExecContainerOptions}; use std::env; +use tokio::prelude::{Future, Stream}; 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", @@ -15,13 +21,18 @@ fn main() { .attach_stdout(true) .attach_stderr(true) .build(); - if let Some(id) = env::args().nth(1) { - match docker.containers().get(&id).exec(&options) { - Ok(res) => { - println!("Stdout: {}", res.stdout); - println!("Stderr: {}", res.stderr); + let fut = docker + .containers() + .get(&id) + .exec(&options) + .for_each(|line| { + match line.stream_type { + StreamType::StdOut => println!("Stdout: {}", line.data), + StreamType::StdErr => eprintln!("Stderr: {}", line.data), } - Err(err) => println!("An error occured: {:?}", err), - } - } + Ok(()) + }) + .map_err(|e| eprintln!("Error: {}", e)); + + tokio::run(fut); } -- cgit v1.2.3