separate function check_missing_formats_when_decompressing

This commit is contained in:
João M. Bezerra 2023-02-03 01:42:45 -03:00
parent db62f1c534
commit f33c9c0f39
2 changed files with 38 additions and 33 deletions

View File

@ -9,7 +9,6 @@ use std::{
use crate::{ use crate::{
error::FinalError, error::FinalError,
extension,
extension::Extension, extension::Extension,
info, info,
utils::{pretty_format_list_of_paths, try_infer_extension, user_wants_to_continue, EscapedPathDisplay}, utils::{pretty_format_list_of_paths, try_infer_extension, user_wants_to_continue, EscapedPathDisplay},
@ -98,7 +97,7 @@ pub fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec<Extension
} }
/// Show error if archive format is not the first format in the chain. /// Show error if archive format is not the first format in the chain.
pub fn check_archive_formats_position(formats: &[extension::Extension], output_path: &Path) -> Result<()> { pub fn check_archive_formats_position(formats: &[Extension], output_path: &Path) -> Result<()> {
if let Some(format) = formats.iter().skip(1).find(|format| format.is_archive()) { if let Some(format) = formats.iter().skip(1).find(|format| format.is_archive()) {
let error = FinalError::with_title(format!( let error = FinalError::with_title(format!(
"Cannot compress to '{}'.", "Cannot compress to '{}'.",
@ -121,3 +120,33 @@ pub fn check_archive_formats_position(formats: &[extension::Extension], output_p
} }
Ok(()) Ok(())
} }
/// Check if all provided files have formats to decompress.
pub fn check_missing_formats_when_decompressing(files: &[PathBuf], formats: &[Vec<Extension>]) -> Result<()> {
let files_missing_format: Vec<PathBuf> = files
.iter()
.zip(formats)
.filter(|(_, format)| format.is_empty())
.map(|(input_path, _)| PathBuf::from(input_path))
.collect();
if let Some(path) = files_missing_format.first() {
let error = FinalError::with_title("Cannot decompress files without extensions")
.detail(format!(
"Files without supported extensions: {}",
pretty_format_list_of_paths(&files_missing_format)
))
.detail("Decompression formats are detected automatically by the file extension")
.hint("Provide a file with a supported extension:")
.hint(" ouch decompress example.tar.gz")
.hint("")
.hint("Or overwrite this option with the '--format' flag:")
.hint(format!(
" ouch decompress {} --format tar.gz",
EscapedPathDisplay::new(path),
));
return Err(error.into());
}
Ok(())
}

View File

@ -10,14 +10,14 @@ use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelI
use utils::colors; use utils::colors;
use crate::{ use crate::{
check::{check_archive_formats_position, check_for_non_archive_formats, check_mime_type}, check,
cli::Subcommand, cli::Subcommand,
commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents}, commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents},
error::{Error, FinalError}, error::{Error, FinalError},
extension::{self, build_archive_file_suggestion, parse_format}, extension::{self, build_archive_file_suggestion, parse_format},
info, info,
list::ListOptions, list::ListOptions,
utils::{self, pretty_format_list_of_paths, to_utf, EscapedPathDisplay, FileVisibilityPolicy}, utils::{self, to_utf, EscapedPathDisplay, FileVisibilityPolicy},
warning, CliArgs, QuestionPolicy, warning, CliArgs, QuestionPolicy,
}; };
@ -117,7 +117,7 @@ pub fn run(
return Err(error.into()); return Err(error.into());
} }
check_archive_formats_position(&formats, &output_path)?; check::check_archive_formats_position(&formats, &output_path)?;
let output_file = match utils::ask_to_create_file(&output_path, question_policy)? { let output_file = match utils::ask_to_create_file(&output_path, question_policy)? {
Some(writer) => writer, Some(writer) => writer,
@ -183,35 +183,11 @@ pub fn run(
} }
} }
if let ControlFlow::Break(_) = check_mime_type(&files, &mut formats, question_policy)? { if let ControlFlow::Break(_) = check::check_mime_type(&files, &mut formats, question_policy)? {
return Ok(()); return Ok(());
} }
let files_missing_format: Vec<PathBuf> = files check::check_missing_formats_when_decompressing(&files, &formats)?;
.iter()
.zip(&formats)
.filter(|(_, formats)| formats.is_empty())
.map(|(input_path, _)| PathBuf::from(input_path))
.collect();
if let Some(path) = files_missing_format.first() {
let error = FinalError::with_title("Cannot decompress files without extensions")
.detail(format!(
"Files without supported extensions: {}",
pretty_format_list_of_paths(&files_missing_format)
))
.detail("Decompression formats are detected automatically by the file extension")
.hint("Provide a file with a supported extension:")
.hint(" ouch decompress example.tar.gz")
.hint("")
.hint("Or overwrite this option with the '--format' flag:")
.hint(format!(
" ouch decompress {} --format tar.gz",
EscapedPathDisplay::new(path),
));
return Err(error.into());
}
// The directory that will contain the output files // The directory that will contain the output files
// We default to the current directory if the user didn't specify an output directory with --dir // We default to the current directory if the user didn't specify an output directory with --dir
@ -252,13 +228,13 @@ pub fn run(
formats.push(file_formats); formats.push(file_formats);
} }
if let ControlFlow::Break(_) = check_mime_type(&files, &mut formats, question_policy)? { if let ControlFlow::Break(_) = check::check_mime_type(&files, &mut formats, question_policy)? {
return Ok(()); return Ok(());
} }
} }
// Ensure we were not told to list the content of a non-archive compressed file // Ensure we were not told to list the content of a non-archive compressed file
check_for_non_archive_formats(&files, &formats)?; check::check_for_non_archive_formats(&files, &formats)?;
let list_options = ListOptions { tree }; let list_options = ListOptions { tree };