Skip to content

Commit

Permalink
feat(mater-cli): allow overwrite on extract (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmg-duarte authored Feb 14, 2025
1 parent 7957571 commit 543a168
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
18 changes: 12 additions & 6 deletions mater/cli/src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ use crate::error::Error;
pub(crate) async fn extract_file_from_car(
input_path: &PathBuf,
output_path: &PathBuf,
overwrite: bool,
) -> Result<(), Error> {
let source_file = File::open(&input_path).await?;
let mut output_file = File::create_new(&output_path).await?;
let mut output_file = if overwrite {
File::create(&output_path).await?
} else {
File::create_new(&output_path).await?
};

let size = source_file.metadata().await?.len();

// Return error if the file is empty (no headers, pragma)
Expand Down Expand Up @@ -54,7 +60,7 @@ mod tests {
let output_path = temp_dir.path().join("output_file");

// Call the function under test
let result = extract_file_from_car(&input_path, &output_path).await;
let result = extract_file_from_car(&input_path, &output_path, false).await;
// Assert the function succeeded
assert!(result.is_ok());

Expand Down Expand Up @@ -86,7 +92,7 @@ mod tests {
let output_path = temp_dir.path().join("output_file");

// Call the function under test
let result = extract_file_from_car(&input_path, &output_path).await;
let result = extract_file_from_car(&input_path, &output_path, false).await;
// Assert the function succeeded
assert!(result.is_ok());

Expand All @@ -113,7 +119,7 @@ mod tests {
let output_path = temp_dir.path().join("test_output/output_file");

// Call the function under test
let result = extract_file_from_car(&input_path, &output_path).await;
let result = extract_file_from_car(&input_path, &output_path, false).await;

// Assert the function returns an error
assert!(result.is_err());
Expand All @@ -134,7 +140,7 @@ mod tests {
File::create(&output_path).await?;

// Call the function under test
let result = extract_file_from_car(&input_path, &output_path).await;
let result = extract_file_from_car(&input_path, &output_path, false).await;

// Assert the function returns an error
assert!(result.is_err());
Expand All @@ -157,7 +163,7 @@ mod tests {
File::create_new(&input_path).await?;

// Call the function under test
let result = extract_file_from_car(&input_path, &output_path).await;
let result = extract_file_from_car(&input_path, &output_path, false).await;

// Assert the function returns an error
assert!(result.is_err());
Expand Down
6 changes: 5 additions & 1 deletion mater/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ enum MaterCli {
input_path: PathBuf,
/// Path to output file
output_path: Option<PathBuf>,
/// If enabled, the output will overwrite any existing files.
#[arg(long, action)]
overwrite: bool,
},
}

Expand Down Expand Up @@ -68,13 +71,14 @@ async fn main() -> Result<(), Error> {
MaterCli::Extract {
input_path,
output_path,
overwrite,
} => {
let output_path = output_path.unwrap_or_else(|| {
let mut new_path = input_path.clone();
new_path.set_extension("");
new_path
});
extract_file_from_car(&input_path, &output_path).await?;
extract_file_from_car(&input_path, &output_path, overwrite).await?;

println!(
"Successfully converted CARv2 file {} and saved it to to {}",
Expand Down

0 comments on commit 543a168

Please sign in to comment.