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
26
27
28
29
30
31
32
use crate::types::{ImageDigest, SandboxError};
use async_process::{Command, Stdio};
use std::error::Error;

pub fn command() -> Command {
    Command::new("podman")
}

pub async fn image_exists(id: &ImageDigest) -> Result<bool, Box<dyn Error>> {
    let mut command = command();
    Ok(command
        .arg("image").arg("exists")
        .arg(id.digest.as_str())
        .status().await?.success())
}

pub async fn pull(id: &ImageDigest) -> Result<(), Box<dyn Error>> {
    let mut command = command();
    let digest = String::from_utf8(
        command
            .arg("pull")
            .arg(id.image.as_str())
            .stderr(Stdio::inherit())
            .output().await?.stdout)?;

    let digest = digest.trim();
    if digest == id.digest.as_str() {
        Ok(())
    } else {
        Err(Box::new(SandboxError::UnexpectedDigest(digest.to_string())))
    }
}