Skip to content

Commit

Permalink
feat(cli): add option to remove source file after decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
ttys3 committed Nov 30, 2024
1 parent 8c32d2c commit a9b452b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,

/// Remove the source file after successful decompression
#[arg(short = 'r', long)]
remove: bool,
},
/// List contents of an archive
#[command(visible_aliases = ["l", "ls"])]
Expand All @@ -97,7 +101,7 @@ pub enum Subcommand {
archives: Vec<PathBuf>,

/// Show archive contents as a tree
#[arg(short, long)]
#[arg(short, long, default_value_t = false)]
tree: bool,
},
}
Expand Down
13 changes: 12 additions & 1 deletion src/commands/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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(());
}

Expand Down Expand Up @@ -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(())
}

Expand Down
10 changes: 9 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![];

Expand Down Expand Up @@ -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,
Expand All @@ -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,
)
})
}
Expand Down

0 comments on commit a9b452b

Please sign in to comment.