From a9b452bf0f78cf01d1ad6a051fa6e7463fa477c1 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 | 6 +++++- src/commands/decompress.rs | 13 ++++++++++++- src/commands/mod.rs | 10 +++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/cli/args.rs b/src/cli/args.rs index 05b522c6..c025fee2 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)] + remove: bool, }, /// List contents of an archive #[command(visible_aliases = ["l", "ls"])] @@ -97,7 +101,7 @@ pub enum Subcommand { archives: Vec, /// Show archive contents as a tree - #[arg(short, long)] + #[arg(short, long, default_value_t = false)] tree: bool, }, } diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index 6c35c250..83917631 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -14,7 +14,7 @@ use crate::{ Extension, }, utils::{ - self, io::lock_and_flush_output_stdio, is_path_stdin, logger::info_accessible, nice_directory_display, + self, io::lock_and_flush_output_stdio, is_path_stdin, logger::info_accessible, logger::info, nice_directory_display, user_wants_to_continue, }, QuestionAction, QuestionPolicy, BUFFER_CAPACITY, @@ -37,6 +37,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 +84,11 @@ 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 +239,11 @@ 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 f2963591..aa5105e4 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -134,7 +134,7 @@ 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 +182,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 +199,7 @@ pub fn run( args.password.as_deref().map(|str| { <[u8] as ByteSlice>::from_os_str(str).expect("convert password to bytes failed") }), + remove, ) }) }