testcontainers-build 0.1.0

Build Testcontainers images from folders, remote contexts, and pluggable Git providers
Documentation
use crate::{BuildContextProvider, BuildOptions, BuiltImage, ImageBuilder, Result};
use std::time::Duration;

/// Configure a build for a source with a fluent API.
/// Configuration methods consume and return the request.
#[must_use]
pub struct ImageBuildRequest<S> {
    builder: ImageBuilder,
    source: S,
    options: BuildOptions,
}

impl<S: BuildContextProvider> ImageBuildRequest<S> {
    pub(crate) fn new(builder: ImageBuilder, source: S) -> Self {
        Self {
            builder,
            source,
            options: BuildOptions::default(),
        }
    }

    /// Replace all build options.
    /// Move the options into this request.
    pub fn with_options(mut self, options: BuildOptions) -> Self {
        self.options = options;
        self
    }

    /// Set the Dockerfile path relative to the context.
    /// Use forward slashes in this path.
    pub fn with_dockerfile(mut self, path: impl Into<String>) -> Self {
        self.options.dockerfile = path.into();
        self
    }

    /// Set or replace one build argument.
    /// Keep all other arguments in this request.
    pub fn with_build_argument(
        mut self,
        name: impl Into<String>,
        value: impl Into<String>,
    ) -> Self {
        self.options
            .build_arguments
            .insert(name.into(), value.into());
        self
    }

    /// Select the final build stage.
    /// Use with_options to clear the selected stage.
    pub fn with_target(mut self, target: impl Into<String>) -> Self {
        self.options.target = Some(target.into());
        self
    }

    /// Select the target platform, such as linux/amd64.
    /// Use with_options to restore the Docker default.
    pub fn with_platform(mut self, platform: impl Into<String>) -> Self {
        self.options.platform = Some(platform.into());
        self
    }

    /// Control the Docker layer cache.
    /// Pass true to disable the cache.
    pub fn with_no_cache(mut self, no_cache: bool) -> Self {
        self.options.no_cache = no_cache;
        self
    }

    /// Set the time limit for source resolution and the build.
    /// The time limit must be positive.
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.options.timeout = timeout;
        self
    }

    /// Resolve the source and build the configured image.
    /// Consume this request and return its unique image tag.
    pub async fn build(self) -> Result<BuiltImage> {
        self.builder.build(&self.source, &self.options).await
    }
}