diff --git a/mater/cli/src/extract.rs b/mater/cli/src/extract.rs index bef6a2ff2..c2881f7db 100644 --- a/mater/cli/src/extract.rs +++ b/mater/cli/src/extract.rs @@ -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) @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); diff --git a/mater/cli/src/main.rs b/mater/cli/src/main.rs index ec00f6068..bc821311b 100644 --- a/mater/cli/src/main.rs +++ b/mater/cli/src/main.rs @@ -36,6 +36,9 @@ enum MaterCli { input_path: PathBuf, /// Path to output file output_path: Option, + /// If enabled, the output will overwrite any existing files. + #[arg(long, action)] + overwrite: bool, }, } @@ -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 {}",