-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support copy files to container (#730)
This PR supports copying files into the container like this: ```rust let container = GenericImage::new("alpine", "latest") .with_wait_for(WaitFor::seconds(2)) .with_copy_to("/tmp/somefile", "foobar".to_string().into_bytes()) .start() .await?; ```
- Loading branch information
Showing
12 changed files
with
237 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::{ | ||
io, | ||
path::{self, Path, PathBuf}, | ||
}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct CopyToContainer { | ||
pub target: String, | ||
pub source: CopyDataSource, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum CopyDataSource { | ||
File(PathBuf), | ||
Data(Vec<u8>), | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum CopyToContaienrError { | ||
#[error("io failed with error: {0}")] | ||
IoError(io::Error), | ||
#[error("failed to get the path name: {0}")] | ||
PathNameError(String), | ||
} | ||
|
||
impl CopyToContainer { | ||
pub fn target_directory(&self) -> Result<String, CopyToContaienrError> { | ||
match path::Path::new(&self.target).parent() { | ||
Some(v) => Ok(v.display().to_string()), | ||
None => return Err(CopyToContaienrError::PathNameError(self.target.clone())), | ||
Check warning on line 30 in testcontainers/src/core/copy.rs GitHub Actions / clippyunneeded `return` statement
|
||
} | ||
} | ||
|
||
pub async fn tar(&self) -> Result<bytes::Bytes, CopyToContaienrError> { | ||
self.source.tar(&self.target).await | ||
} | ||
} | ||
|
||
impl CopyDataSource { | ||
pub async fn tar( | ||
&self, | ||
target_path: impl Into<String>, | ||
) -> Result<bytes::Bytes, CopyToContaienrError> { | ||
let target_path: String = target_path.into(); | ||
let mut ar = tokio_tar::Builder::new(Vec::new()); | ||
|
||
match self { | ||
CopyDataSource::File(file_path) => { | ||
let mut f = &mut tokio::fs::File::open(file_path) | ||
.await | ||
.map_err(CopyToContaienrError::IoError)?; | ||
ar.append_file(&target_path, &mut f) | ||
Check warning on line 52 in testcontainers/src/core/copy.rs GitHub Actions / clippythis expression creates a reference which is immediately dereferenced by the compiler
|
||
.await | ||
.map_err(CopyToContaienrError::IoError)?; | ||
} | ||
CopyDataSource::Data(data) => { | ||
let path = path::Path::new(&target_path); | ||
let file_name = match path.file_name() { | ||
Some(v) => v, | ||
None => return Err(CopyToContaienrError::PathNameError(target_path)), | ||
}; | ||
|
||
let mut header = tokio_tar::Header::new_gnu(); | ||
header.set_size(data.len() as u64); | ||
header.set_mode(0o0644); | ||
header.set_cksum(); | ||
|
||
ar.append_data(&mut header, file_name, data.as_slice()) | ||
.await | ||
.map_err(CopyToContaienrError::IoError)?; | ||
} | ||
} | ||
|
||
let bytes = ar | ||
.into_inner() | ||
.await | ||
.map_err(CopyToContaienrError::IoError)?; | ||
|
||
Ok(bytes::Bytes::copy_from_slice(bytes.as_slice())) | ||
} | ||
} | ||
|
||
impl From<&Path> for CopyDataSource { | ||
fn from(value: &Path) -> Self { | ||
CopyDataSource::File(value.to_path_buf()) | ||
} | ||
} | ||
impl From<PathBuf> for CopyDataSource { | ||
fn from(value: PathBuf) -> Self { | ||
CopyDataSource::File(value) | ||
} | ||
} | ||
impl From<Vec<u8>> for CopyDataSource { | ||
fn from(value: Vec<u8>) -> Self { | ||
CopyDataSource::Data(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters