Skip to content

Commit 4b918d9

Browse files
committed
refactor: Add BuildContextBuilder to replace public copy::*
Signed-off-by: mbodmer <marc.bodmer@email.ch>
1 parent 2d25378 commit 4b918d9

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

testcontainers/src/buildables/generic.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
11
use std::path::PathBuf;
22

3-
use crate::{core::copy::CopyToContainerCollection, BuildableImage, CopyToContainer, GenericImage};
3+
use crate::{
4+
core::{copy::CopyToContainerCollection, BuildContextBuilder},
5+
BuildableImage, GenericImage,
6+
};
47

58
#[derive(Debug)]
69
pub struct GenericBuildableImage {
710
name: String,
811
tag: String,
9-
build_context: CopyToContainerCollection,
12+
build_context_builder: BuildContextBuilder,
1013
}
1114

1215
impl GenericBuildableImage {
1316
pub fn new(name: impl Into<String>, tag: impl Into<String>) -> Self {
1417
Self {
1518
name: name.into(),
1619
tag: tag.into(),
17-
build_context: CopyToContainerCollection::new(vec![]),
20+
build_context_builder: BuildContextBuilder::default(),
1821
}
1922
}
2023

21-
pub fn with_dockerfile(self, source: impl Into<PathBuf>) -> Self {
22-
self.with_file(source.into(), "Dockerfile")
24+
pub fn with_dockerfile(mut self, source: impl Into<PathBuf>) -> Self {
25+
self.build_context_builder = self.build_context_builder.with_dockerfile(source);
26+
self
2327
}
2428

25-
pub fn with_dockerfile_string(self, content: impl Into<String>) -> Self {
26-
self.with_data(content.into(), "Dockerfile")
29+
pub fn with_dockerfile_string(mut self, content: impl Into<String>) -> Self {
30+
self.build_context_builder = self.build_context_builder.with_dockerfile_string(content);
31+
self
2732
}
2833

2934
pub fn with_file(mut self, source: impl Into<PathBuf>, target: impl Into<String>) -> Self {
30-
self.build_context
31-
.add(CopyToContainer::new(source.into(), target));
35+
self.build_context_builder = self.build_context_builder.with_file(source, target);
3236
self
3337
}
3438

3539
pub fn with_data(mut self, data: impl Into<Vec<u8>>, target: impl Into<String>) -> Self {
36-
self.build_context
37-
.add(CopyToContainer::new(data.into(), target));
40+
self.build_context_builder = self.build_context_builder.with_data(data, target);
3841
self
3942
}
4043
}
@@ -43,7 +46,7 @@ impl BuildableImage for GenericBuildableImage {
4346
type Built = GenericImage;
4447

4548
fn build_context(&self) -> CopyToContainerCollection {
46-
self.build_context.clone()
49+
self.build_context_builder.as_copy_to_container_collection()
4750
}
4851

4952
fn descriptor(&self) -> String {

testcontainers/src/core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[cfg(feature = "reusable-containers")]
22
pub use self::image::ReuseDirective;
33
pub use self::{
4+
build_context::BuildContextBuilder,
45
buildable::BuildableImage,
56
containers::*,
67
copy::{CopyDataSource, CopyToContainer, CopyToContainerCollection, CopyToContainerError},
@@ -14,6 +15,7 @@ mod buildable;
1415
mod image;
1516

1617
pub(crate) mod async_drop;
18+
pub(crate) mod build_context;
1719
pub mod client;
1820
pub(crate) mod containers;
1921
pub(crate) mod copy;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std::path::PathBuf;
2+
3+
use crate::{core::copy::CopyToContainerCollection, CopyToContainer};
4+
5+
#[derive(Debug, Default, Clone)]
6+
pub struct BuildContextBuilder {
7+
build_context_parts: Vec<CopyToContainer>,
8+
}
9+
10+
impl BuildContextBuilder {
11+
pub fn with_dockerfile(self, source: impl Into<PathBuf>) -> Self {
12+
self.with_file(source.into(), "Dockerfile")
13+
}
14+
15+
pub fn with_dockerfile_string(self, content: impl Into<String>) -> Self {
16+
self.with_data(content.into(), "Dockerfile")
17+
}
18+
19+
pub fn with_file(mut self, source: impl Into<PathBuf>, target: impl Into<String>) -> Self {
20+
self.build_context_parts
21+
.push(CopyToContainer::new(source.into(), target));
22+
self
23+
}
24+
25+
pub fn with_data(mut self, data: impl Into<Vec<u8>>, target: impl Into<String>) -> Self {
26+
self.build_context_parts
27+
.push(CopyToContainer::new(data.into(), target));
28+
self
29+
}
30+
31+
pub fn collect(self) -> CopyToContainerCollection {
32+
CopyToContainerCollection::new(self.build_context_parts)
33+
}
34+
35+
pub fn as_copy_to_container_collection(&self) -> CopyToContainerCollection {
36+
CopyToContainerCollection::new(self.build_context_parts.clone())
37+
}
38+
}

0 commit comments

Comments
 (0)