feat(cli): add option to remove source file after decompression

This commit is contained in:
ttyS3 2024-11-30 16:00:50 +00:00 committed by João Marcos
parent 493213e393
commit 55aa05b631
3 changed files with 43 additions and 3 deletions

View File

@ -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, 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()
}

View File

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

View File

@ -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,
)
})
}