testcontainers-build 0.1.0

Build Testcontainers images from folders, remote contexts, and pluggable Git providers
Documentation
use crate::{Error, Result, validate_relative};
use std::{collections::BTreeMap, time::Duration};

/// Set the image build options.
/// Use Default to set a ten-minute timeout.
#[derive(Debug, Clone)]
pub struct BuildOptions {
    /// Set the Dockerfile path.
    /// Use a path relative to the build context.
    pub dockerfile: String,
    /// Set Docker build arguments.
    /// Do not use these values for secrets.
    pub build_arguments: BTreeMap<String, String>,
    /// Set the build stage.
    /// Use None for the last stage.
    pub target: Option<String>,
    /// Set the target platform.
    /// Use None for the Docker default.
    pub platform: Option<String>,
    /// Disable the Docker layer cache.
    /// The default is false.
    pub no_cache: bool,
    /// Set the maximum operation time.
    /// Include source resolution and the image build.
    pub timeout: Duration,
}

impl BuildOptions {
    pub(crate) fn validate(&self) -> Result<()> {
        validate_relative(&self.dockerfile)?;
        if self.timeout.is_zero() || self.timeout > Duration::from_millis(u32::MAX as u64 - 1) {
            return Err(Error::InvalidInput(
                "use a positive timeout of at most 4294967294 milliseconds".into(),
            ));
        }
        for (key, value) in &self.build_arguments {
            if key.trim().is_empty() || key.contains(['=', '\0']) || value.contains('\0') {
                return Err(Error::InvalidInput("invalid build argument".into()));
            }
        }
        for value in [&self.target, &self.platform].into_iter().flatten() {
            if value.trim().is_empty() || value.contains('\0') {
                return Err(Error::InvalidInput(
                    "invalid target stage or platform".into(),
                ));
            }
        }
        Ok(())
    }
}

impl Default for BuildOptions {
    fn default() -> Self {
        Self {
            dockerfile: "Dockerfile".into(),
            build_arguments: BTreeMap::new(),
            target: None,
            platform: None,
            no_cache: false,
            timeout: Duration::from_secs(600),
        }
    }
}