diff options
Diffstat (limited to 'examples/export.rs')
-rw-r--r-- | examples/export.rs | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/examples/export.rs b/examples/export.rs index 55d1b7b..34f460d 100644 --- a/examples/export.rs +++ b/examples/export.rs @@ -1,8 +1,10 @@ -use shiplift::{errors::Error, Docker}; +use futures::StreamExt; use std::{env, fs::OpenOptions, io::Write}; -use tokio::prelude::{Future, Stream}; -fn main() { +use shiplift::{errors::Error, Docker}; + +#[tokio::main] +async fn main() { let docker = Docker::new(); let id = env::args().nth(1).expect("You need to specify an image id"); @@ -11,17 +13,13 @@ fn main() { .create(true) .open(format!("{}.tar", &id)) .unwrap(); + let images = docker.images(); - let fut = images - .get(&id) - .export() - .for_each(move |bytes| { - export_file - .write(&bytes[..]) - .map(|n| println!("copied {} bytes", n)) - .map_err(Error::IO) - }) - .map_err(|e| eprintln!("Error: {}", e)); - tokio::run(fut) + while let Some(export_result) = images.get(&id).export().next().await { + match export_result.and_then(|bytes| export_file.write(&bytes).map_err(Error::from)) { + Ok(n) => println!("copied {} bytes", n), + Err(e) => eprintln!("Error: {}", e), + } + } } |