Skip to main content

testcontainers_build/
request.rs

1use crate::{BuildContextProvider, BuildOptions, BuiltImage, ImageBuilder, Result};
2use std::time::Duration;
3
4/// Configure a build for a source with a fluent API.
5/// Configuration methods consume and return the request.
6#[must_use]
7pub struct ImageBuildRequest<S> {
8    builder: ImageBuilder,
9    source: S,
10    options: BuildOptions,
11}
12
13impl<S: BuildContextProvider> ImageBuildRequest<S> {
14    pub(crate) fn new(builder: ImageBuilder, source: S) -> Self {
15        Self {
16            builder,
17            source,
18            options: BuildOptions::default(),
19        }
20    }
21
22    /// Replace all build options.
23    /// Move the options into this request.
24    pub fn with_options(mut self, options: BuildOptions) -> Self {
25        self.options = options;
26        self
27    }
28
29    /// Set the Dockerfile path relative to the context.
30    /// Use forward slashes in this path.
31    pub fn with_dockerfile(mut self, path: impl Into<String>) -> Self {
32        self.options.dockerfile = path.into();
33        self
34    }
35
36    /// Set or replace one build argument.
37    /// Keep all other arguments in this request.
38    pub fn with_build_argument(
39        mut self,
40        name: impl Into<String>,
41        value: impl Into<String>,
42    ) -> Self {
43        self.options
44            .build_arguments
45            .insert(name.into(), value.into());
46        self
47    }
48
49    /// Select the final build stage.
50    /// Use with_options to clear the selected stage.
51    pub fn with_target(mut self, target: impl Into<String>) -> Self {
52        self.options.target = Some(target.into());
53        self
54    }
55
56    /// Select the target platform, such as linux/amd64.
57    /// Use with_options to restore the Docker default.
58    pub fn with_platform(mut self, platform: impl Into<String>) -> Self {
59        self.options.platform = Some(platform.into());
60        self
61    }
62
63    /// Control the Docker layer cache.
64    /// Pass true to disable the cache.
65    pub fn with_no_cache(mut self, no_cache: bool) -> Self {
66        self.options.no_cache = no_cache;
67        self
68    }
69
70    /// Set the time limit for source resolution and the build.
71    /// The time limit must be positive.
72    pub fn with_timeout(mut self, timeout: Duration) -> Self {
73        self.options.timeout = timeout;
74        self
75    }
76
77    /// Resolve the source and build the configured image.
78    /// Consume this request and return its unique image tag.
79    pub async fn build(self) -> Result<BuiltImage> {
80        self.builder.build(&self.source, &self.options).await
81    }
82}