summaryrefslogtreecommitdiffstats
path: root/examples/export.rs
blob: 34f460d54cb47bacce51968a237b6cd90a8bb313 (plain)
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
use futures::StreamExt;
use std::{env, fs::OpenOptions, io::Write};

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");

    let mut export_file = OpenOptions::new()
        .write(true)
        .create(true)
        .open(format!("{}.tar", &id))
        .unwrap();

    let images = docker.images();

    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),
        }
    }
}