From 55aa05b631cd7348ed99fe71c547242f34b8a087 Mon Sep 17 00:00:00 2001 From: ttyS3 Date: Sat, 30 Nov 2024 16:00:50 +0000 Subject: [PATCH] feat(cli): add option to remove source file after decompression --- src/cli/args.rs | 8 ++++++++ src/commands/decompress.rs | 24 ++++++++++++++++++++++-- src/commands/mod.rs | 14 +++++++++++++- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/cli/args.rs b/src/cli/args.rs index 05b522c..c07e85c 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -88,6 +88,10 @@ pub enum Subcommand { /// Place results in a directory other than the current one #[arg(short = 'd', long = "dir", value_hint = ValueHint::FilePath)] output_dir: Option, + + /// Remove the source file after successful decompression + #[arg(short = 'r', long, default_value_t = false)] + remove: bool, }, /// List contents of an archive #[command(visible_aliases = ["l", "ls"])] @@ -142,6 +146,7 @@ mod tests { // Put a crazy value here so no test can assert it unintentionally files: vec!["\x00\x11\x22".into()], output_dir: None, + remove: false, }, } } @@ -154,6 +159,7 @@ mod tests { cmd: Subcommand::Decompress { files: to_paths(["file.tar.gz"]), output_dir: None, + remove: false, }, ..mock_cli_args() } @@ -164,6 +170,7 @@ mod tests { cmd: Subcommand::Decompress { files: to_paths(["file.tar.gz"]), output_dir: None, + remove: false, }, ..mock_cli_args() } @@ -174,6 +181,7 @@ mod tests { cmd: Subcommand::Decompress { files: to_paths(["a", "b", "c"]), output_dir: None, + remove: false, }, ..mock_cli_args() } diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index 6c35c25..a1d7e84 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -14,8 +14,11 @@ use crate::{ Extension, }, utils::{ - self, io::lock_and_flush_output_stdio, is_path_stdin, logger::info_accessible, nice_directory_display, - user_wants_to_continue, + self, + io::lock_and_flush_output_stdio, + is_path_stdin, + logger::{info, info_accessible}, + nice_directory_display, user_wants_to_continue, }, QuestionAction, QuestionPolicy, BUFFER_CAPACITY, }; @@ -37,6 +40,7 @@ pub fn decompress_file( question_policy: QuestionPolicy, quiet: bool, password: Option<&[u8]>, + remove: bool, ) -> crate::Result<()> { assert!(output_dir.exists()); let input_is_stdin = is_path_stdin(input_file_path); @@ -83,6 +87,14 @@ pub fn decompress_file( files_unpacked )); + if !input_is_stdin && remove { + fs::remove_file(input_file_path)?; + info(format!( + "Removed input file {}", + nice_directory_display(input_file_path) + )); + } + return Ok(()); } @@ -233,6 +245,14 @@ pub fn decompress_file( )); info_accessible(format!("Files unpacked: {}", files_unpacked)); + if !input_is_stdin && remove { + fs::remove_file(input_file_path)?; + info(format!( + "Removed input file {}", + nice_directory_display(input_file_path) + )); + } + Ok(()) } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index f296359..a4b8fca 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -134,7 +134,11 @@ pub fn run( compress_result.map(|_| ()) } - Subcommand::Decompress { files, output_dir } => { + Subcommand::Decompress { + files, + output_dir, + remove, + } => { let mut output_paths = vec![]; let mut formats = vec![]; @@ -182,6 +186,13 @@ pub fn run( } else { output_dir.join(file_name) }; + info_accessible(format!( + "begin decompress file ... input_path: {}, formats: {:?}, file_name: {}, output_file_path: {}", + path_to_str(input_path), + formats, + path_to_str(file_name), + path_to_str(&output_file_path) + )); decompress_file( input_path, formats, @@ -192,6 +203,7 @@ pub fn run( args.password.as_deref().map(|str| { <[u8] as ByteSlice>::from_os_str(str).expect("convert password to bytes failed") }), + remove, ) }) }