mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
feat(cli): add option to remove source file after decompression
This commit is contained in:
parent
493213e393
commit
55aa05b631
@ -88,6 +88,10 @@ pub enum Subcommand {
|
|||||||
/// Place results in a directory other than the current one
|
/// Place results in a directory other than the current one
|
||||||
#[arg(short = 'd', long = "dir", value_hint = ValueHint::FilePath)]
|
#[arg(short = 'd', long = "dir", value_hint = ValueHint::FilePath)]
|
||||||
output_dir: Option<PathBuf>,
|
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
|
/// List contents of an archive
|
||||||
#[command(visible_aliases = ["l", "ls"])]
|
#[command(visible_aliases = ["l", "ls"])]
|
||||||
@ -142,6 +146,7 @@ mod tests {
|
|||||||
// Put a crazy value here so no test can assert it unintentionally
|
// Put a crazy value here so no test can assert it unintentionally
|
||||||
files: vec!["\x00\x11\x22".into()],
|
files: vec!["\x00\x11\x22".into()],
|
||||||
output_dir: None,
|
output_dir: None,
|
||||||
|
remove: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -154,6 +159,7 @@ mod tests {
|
|||||||
cmd: Subcommand::Decompress {
|
cmd: Subcommand::Decompress {
|
||||||
files: to_paths(["file.tar.gz"]),
|
files: to_paths(["file.tar.gz"]),
|
||||||
output_dir: None,
|
output_dir: None,
|
||||||
|
remove: false,
|
||||||
},
|
},
|
||||||
..mock_cli_args()
|
..mock_cli_args()
|
||||||
}
|
}
|
||||||
@ -164,6 +170,7 @@ mod tests {
|
|||||||
cmd: Subcommand::Decompress {
|
cmd: Subcommand::Decompress {
|
||||||
files: to_paths(["file.tar.gz"]),
|
files: to_paths(["file.tar.gz"]),
|
||||||
output_dir: None,
|
output_dir: None,
|
||||||
|
remove: false,
|
||||||
},
|
},
|
||||||
..mock_cli_args()
|
..mock_cli_args()
|
||||||
}
|
}
|
||||||
@ -174,6 +181,7 @@ mod tests {
|
|||||||
cmd: Subcommand::Decompress {
|
cmd: Subcommand::Decompress {
|
||||||
files: to_paths(["a", "b", "c"]),
|
files: to_paths(["a", "b", "c"]),
|
||||||
output_dir: None,
|
output_dir: None,
|
||||||
|
remove: false,
|
||||||
},
|
},
|
||||||
..mock_cli_args()
|
..mock_cli_args()
|
||||||
}
|
}
|
||||||
|
@ -14,8 +14,11 @@ use crate::{
|
|||||||
Extension,
|
Extension,
|
||||||
},
|
},
|
||||||
utils::{
|
utils::{
|
||||||
self, io::lock_and_flush_output_stdio, is_path_stdin, logger::info_accessible, nice_directory_display,
|
self,
|
||||||
user_wants_to_continue,
|
io::lock_and_flush_output_stdio,
|
||||||
|
is_path_stdin,
|
||||||
|
logger::{info, info_accessible},
|
||||||
|
nice_directory_display, user_wants_to_continue,
|
||||||
},
|
},
|
||||||
QuestionAction, QuestionPolicy, BUFFER_CAPACITY,
|
QuestionAction, QuestionPolicy, BUFFER_CAPACITY,
|
||||||
};
|
};
|
||||||
@ -37,6 +40,7 @@ pub fn decompress_file(
|
|||||||
question_policy: QuestionPolicy,
|
question_policy: QuestionPolicy,
|
||||||
quiet: bool,
|
quiet: bool,
|
||||||
password: Option<&[u8]>,
|
password: Option<&[u8]>,
|
||||||
|
remove: bool,
|
||||||
) -> crate::Result<()> {
|
) -> crate::Result<()> {
|
||||||
assert!(output_dir.exists());
|
assert!(output_dir.exists());
|
||||||
let input_is_stdin = is_path_stdin(input_file_path);
|
let input_is_stdin = is_path_stdin(input_file_path);
|
||||||
@ -83,6 +87,14 @@ pub fn decompress_file(
|
|||||||
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)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,6 +245,14 @@ pub fn decompress_file(
|
|||||||
));
|
));
|
||||||
info_accessible(format!("Files unpacked: {}", files_unpacked));
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,7 +134,11 @@ pub fn run(
|
|||||||
|
|
||||||
compress_result.map(|_| ())
|
compress_result.map(|_| ())
|
||||||
}
|
}
|
||||||
Subcommand::Decompress { files, output_dir } => {
|
Subcommand::Decompress {
|
||||||
|
files,
|
||||||
|
output_dir,
|
||||||
|
remove,
|
||||||
|
} => {
|
||||||
let mut output_paths = vec![];
|
let mut output_paths = vec![];
|
||||||
let mut formats = vec![];
|
let mut formats = vec![];
|
||||||
|
|
||||||
@ -182,6 +186,13 @@ pub fn run(
|
|||||||
} else {
|
} else {
|
||||||
output_dir.join(file_name)
|
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(
|
decompress_file(
|
||||||
input_path,
|
input_path,
|
||||||
formats,
|
formats,
|
||||||
@ -192,6 +203,7 @@ pub fn run(
|
|||||||
args.password.as_deref().map(|str| {
|
args.password.as_deref().map(|str| {
|
||||||
<[u8] as ByteSlice>::from_os_str(str).expect("convert password to bytes failed")
|
<[u8] as ByteSlice>::from_os_str(str).expect("convert password to bytes failed")
|
||||||
}),
|
}),
|
||||||
|
remove,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user