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