mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 19:45:29 +00:00
Merge pull request #42 from vrmiguel/const-confirmation
Reuse Confirmation struct when checking for overwrite permission
This commit is contained in:
commit
2746f42db9
31
src/cli.rs
31
src/cli.rs
@ -37,8 +37,8 @@ pub fn parse_args() -> crate::Result<ParsedArgs> {
|
||||
match &mut parsed_args.command {
|
||||
Command::Compress { files, .. } | Command::Decompress { files, .. } => {
|
||||
*files = canonicalize_files(&files)?;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
Ok(parsed_args)
|
||||
}
|
||||
@ -72,7 +72,7 @@ fn canonicalize(path: impl AsRef<Path>) -> crate::Result<PathBuf> {
|
||||
} else {
|
||||
Err(io_err.into())
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -107,7 +107,7 @@ pub fn parse_args_from(mut args: Vec<OsString>) -> crate::Result<ParsedArgs> {
|
||||
|
||||
let command = Command::Compress { files, compressed_output_path };
|
||||
ParsedArgs { command, flags }
|
||||
}
|
||||
},
|
||||
// Defaults to decompression when there is no subcommand
|
||||
None => {
|
||||
flags_info.push(arg_flag!('o', "output"));
|
||||
@ -130,7 +130,7 @@ pub fn parse_args_from(mut args: Vec<OsString>) -> crate::Result<ParsedArgs> {
|
||||
|
||||
let command = Command::Decompress { files, output_folder };
|
||||
ParsedArgs { command, flags }
|
||||
}
|
||||
},
|
||||
_ => unreachable!("You should match each subcommand passed."),
|
||||
};
|
||||
|
||||
@ -157,20 +157,14 @@ mod tests {
|
||||
assert_eq!(test_cli("--help").unwrap().command, Command::ShowHelp);
|
||||
assert_eq!(test_cli("--version").unwrap().command, Command::ShowVersion);
|
||||
assert_eq!(test_cli("--version").unwrap().flags, oof::Flags::default());
|
||||
assert_eq!(
|
||||
test_cli("foo.zip bar.zip").unwrap().command,
|
||||
Command::Decompress {
|
||||
assert_eq!(test_cli("foo.zip bar.zip").unwrap().command, Command::Decompress {
|
||||
files: vec!["foo.zip".into(), "bar.zip".into()],
|
||||
output_folder: None
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
test_cli("compress foo bar baz.zip").unwrap().command,
|
||||
Command::Compress {
|
||||
});
|
||||
assert_eq!(test_cli("compress foo bar baz.zip").unwrap().command, Command::Compress {
|
||||
files: vec!["foo".into(), "bar".into()],
|
||||
compressed_output_path: "baz.zip".into()
|
||||
}
|
||||
);
|
||||
});
|
||||
assert_eq!(test_cli("compress").unwrap_err(), crate::Error::MissingArgumentsForCompression);
|
||||
}
|
||||
|
||||
@ -180,12 +174,9 @@ mod tests {
|
||||
assert_eq!(test_cli("--help").unwrap().flags, oof::Flags::default());
|
||||
assert_eq!(test_cli("--version").unwrap().flags, oof::Flags::default());
|
||||
|
||||
assert_eq!(
|
||||
test_cli("foo --yes bar --output folder").unwrap().flags,
|
||||
oof::Flags {
|
||||
assert_eq!(test_cli("foo --yes bar --output folder").unwrap().flags, oof::Flags {
|
||||
boolean_flags: vec!["yes"].into_iter().collect(),
|
||||
argument_flags: vec![("output", OsString::from("folder"))].into_iter().collect(),
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,6 @@ use crate::{
|
||||
BzipDecompressor, DecompressionResult, Decompressor, GzipDecompressor, LzmaDecompressor,
|
||||
TarDecompressor, ZipDecompressor,
|
||||
},
|
||||
dialogs::Confirmation,
|
||||
extension::{CompressionFormat, Extension},
|
||||
file::File,
|
||||
oof, utils,
|
||||
@ -140,8 +139,7 @@ fn decompress_file_in_memory(
|
||||
println!("{}[INFO]{} saving to {:?}.", colors::yellow(), colors::reset(), file_name);
|
||||
|
||||
if file_name.exists() {
|
||||
let confirm = Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE"));
|
||||
if !utils::permission_for_overwriting(&file_name, flags, &confirm)? {
|
||||
if !utils::permission_for_overwriting(&file_name, flags)? {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
@ -169,10 +167,9 @@ fn compress_files(
|
||||
) -> crate::Result<()> {
|
||||
let mut output = File::from(output_path)?;
|
||||
|
||||
let confirm = Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE"));
|
||||
let (first_compressor, second_compressor) = get_compressor(&output)?;
|
||||
|
||||
if output_path.exists() && !utils::permission_for_overwriting(&output_path, flags, &confirm)? {
|
||||
if output_path.exists() && !utils::permission_for_overwriting(&output_path, flags)? {
|
||||
// The user does not want to overwrite the file
|
||||
return Ok(());
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ use tar::{self, Archive};
|
||||
use utils::colors;
|
||||
|
||||
use super::decompressor::{DecompressionResult, Decompressor};
|
||||
use crate::{dialogs::Confirmation, file::File, oof, utils};
|
||||
use crate::{file::File, oof, utils};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TarDecompressor;
|
||||
@ -22,7 +22,6 @@ impl TarDecompressor {
|
||||
&from.path
|
||||
);
|
||||
let mut files_unpacked = vec![];
|
||||
let confirm = Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE"));
|
||||
|
||||
let mut archive: Archive<Box<dyn Read>> = match from.contents_in_memory {
|
||||
Some(bytes) => tar::Archive::new(Box::new(Cursor::new(bytes))),
|
||||
@ -36,9 +35,7 @@ impl TarDecompressor {
|
||||
let mut file = file?;
|
||||
|
||||
let file_path = PathBuf::from(into).join(file.path()?);
|
||||
if file_path.exists()
|
||||
&& !utils::permission_for_overwriting(&file_path, flags, &confirm)?
|
||||
{
|
||||
if file_path.exists() && !utils::permission_for_overwriting(&file_path, flags)? {
|
||||
// The user does not want to overwrite the file
|
||||
continue;
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ use utils::colors;
|
||||
use zip::{self, read::ZipFile, ZipArchive};
|
||||
|
||||
use super::decompressor::{DecompressionResult, Decompressor};
|
||||
use crate::{dialogs::Confirmation, file::File, oof, utils};
|
||||
use crate::{file::File, oof, utils};
|
||||
|
||||
#[cfg(unix)]
|
||||
fn __unix_set_permissions(file_path: &Path, file: &ZipFile) {
|
||||
@ -43,7 +43,6 @@ impl ZipDecompressor {
|
||||
where
|
||||
R: Read + Seek,
|
||||
{
|
||||
let confirm = Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE"));
|
||||
let mut unpacked_files = vec![];
|
||||
for idx in 0..archive.len() {
|
||||
let mut file = archive.by_index(idx)?;
|
||||
@ -53,9 +52,7 @@ impl ZipDecompressor {
|
||||
};
|
||||
|
||||
let file_path = into.join(file_path);
|
||||
if file_path.exists()
|
||||
&& !utils::permission_for_overwriting(&file_path, flags, &confirm)?
|
||||
{
|
||||
if file_path.exists() && !utils::permission_for_overwriting(&file_path, flags)? {
|
||||
// The user does not want to overwrite the file
|
||||
continue;
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ pub struct Confirmation<'a> {
|
||||
pub struct Error;
|
||||
|
||||
impl<'a> Confirmation<'a> {
|
||||
pub fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self {
|
||||
pub const fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self {
|
||||
Self { prompt, placeholder: pattern }
|
||||
}
|
||||
|
||||
|
@ -12,9 +12,12 @@ mod extension;
|
||||
mod file;
|
||||
mod utils;
|
||||
|
||||
use dialogs::Confirmation;
|
||||
pub use error::{Error, Result};
|
||||
|
||||
const VERSION: &str = "0.1.5";
|
||||
const OVERWRITE_CONFIRMATION: Confirmation =
|
||||
Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE"));
|
||||
|
||||
fn help_command() {
|
||||
use utils::colors::*;
|
||||
|
10
src/utils.rs
10
src/utils.rs
@ -5,7 +5,7 @@ use std::{
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::{dialogs::Confirmation, extension::CompressionFormat, file::File, oof};
|
||||
use crate::{extension::CompressionFormat, file::File, oof, OVERWRITE_CONFIRMATION};
|
||||
|
||||
#[macro_export]
|
||||
#[cfg(debug_assertions)]
|
||||
@ -95,11 +95,7 @@ pub fn change_dir_and_return_parent(filename: &Path) -> crate::Result<PathBuf> {
|
||||
Ok(previous_location)
|
||||
}
|
||||
|
||||
pub fn permission_for_overwriting(
|
||||
path: &Path,
|
||||
flags: &oof::Flags,
|
||||
confirm: &Confirmation,
|
||||
) -> crate::Result<bool> {
|
||||
pub fn permission_for_overwriting(path: &Path, flags: &oof::Flags) -> crate::Result<bool> {
|
||||
match (flags.is_present("yes"), flags.is_present("no")) {
|
||||
(true, true) => {
|
||||
unreachable!(
|
||||
@ -112,7 +108,7 @@ pub fn permission_for_overwriting(
|
||||
}
|
||||
|
||||
let file_path_str = to_utf(path);
|
||||
confirm.ask(Some(&file_path_str))
|
||||
OVERWRITE_CONFIRMATION.ask(Some(&file_path_str))
|
||||
}
|
||||
|
||||
pub fn to_utf(os_str: impl AsRef<OsStr>) -> String {
|
||||
|
Loading…
x
Reference in New Issue
Block a user