Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when compressing folder with single-file formats #303

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Categories Used:
- Check for errors when setting the last modified time [\#278](https://github.com/ouch-org/ouch/pull/278) ([marcospb19](https://github.com/marcospb19))
- Use to the humansize crate for formatting human-readable file sizes [\#281](https://github.com/ouch-org/ouch/pull/281) ([figsoda](https://github.com/figsoda))
- Reactivate CI targets for ARM Linux and Windows MinGW [\#289](https://github.com/ouch-org/ouch/pull/289) ([figsoda](https://github.com/figsoda))
- Improve error message when compressing folder with single-file formats [\#303](https://github.com/ouch-org/ouch/pull/303) ([marcospb19](https://github.com/marcospb19))

### Tweaks

Expand Down
52 changes: 23 additions & 29 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use crate::{
info,
list::ListOptions,
utils::{
self, dir_is_empty, pretty_format_list_of_paths, to_utf, try_infer_extension, user_wants_to_continue,
FileVisibilityPolicy,
self, pretty_format_list_of_paths, to_utf, try_infer_extension, user_wants_to_continue, FileVisibilityPolicy,
},
warning, Opts, QuestionAction, QuestionPolicy, Subcommand,
};
Expand All @@ -34,16 +33,6 @@ fn warn_user_about_loading_zip_in_memory() {
warning!("{}", ZIP_IN_MEMORY_LIMITATION_WARNING);
}

fn represents_several_files(files: &[PathBuf]) -> bool {
let is_non_empty_dir = |path: &PathBuf| {
let is_non_empty = || !dir_is_empty(path);

path.is_dir().then(is_non_empty).unwrap_or_default()
};

files.iter().any(is_non_empty_dir) || files.len() > 1
}

/// Builds a suggested output file in scenarios where the user tried to compress
/// a folder into a non-archive compression format, for error message purposes
///
Expand Down Expand Up @@ -100,39 +89,44 @@ pub fn run(
// Formats from path extension, like "file.tar.gz.xz" -> vec![Tar, Gzip, Lzma]
let formats = extension::extensions_from_path(&output_path);

if formats.is_empty() {
let error = FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path)))
let first_format = formats.first().ok_or_else(|| {
Copy link
Member Author

@marcospb19 marcospb19 Oct 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool 💯

let output_path = to_utf(&output_path);
FinalError::with_title(format!("Cannot compress to '{output_path}'."))
.detail("You shall supply the compression format")
.hint("Try adding supported extensions (see --help):")
.hint(format!(" ouch compress <FILES>... {}.tar.gz", to_utf(&output_path)))
.hint(format!(" ouch compress <FILES>... {}.zip", to_utf(&output_path)))
.hint(format!(" ouch compress <FILES>... {output_path}.tar.gz"))
.hint(format!(" ouch compress <FILES>... {output_path}.zip"))
.hint("")
.hint("Alternatively, you can overwrite this option by using the '--format' flag:")
.hint(format!(
" ouch compress <FILES>... {} --format tar.gz",
to_utf(&output_path)
));
.hint(format!(" ouch compress <FILES>... {output_path} --format tar.gz"))
})?;

return Err(error.into());
}
let is_some_input_a_folder = files.iter().any(|path| path.is_dir());
let is_multiple_inputs = files.len() > 1;

if !formats.get(0).map(Extension::is_archive).unwrap_or(false) && represents_several_files(&files) {
// If first format is not archive, can't compress folder, or multiple files
// Index safety: empty formats should be checked above.
if !first_format.is_archive() && (is_some_input_a_folder || is_multiple_inputs) {
// This piece of code creates a suggestion for compressing multiple files
// It says:
// Change from file.bz.xz
// To file.tar.bz.xz
let suggested_output_path = build_archive_file_suggestion(&output_path, ".tar")
.expect("output path did not contain a compression format");

.expect("output path should contain a compression format");
let output_path = to_utf(&output_path);
let first_detail_message = if is_multiple_inputs {
"You are trying to compress multiple files."
} else {
"You are trying to compress a folder."
};

let error = FinalError::with_title(format!("Cannot compress to '{}'.", output_path))
.detail("You are trying to compress multiple files.")
.detail(first_detail_message)
.detail(format!(
"The compression format '{}' cannot receive multiple files.",
"The compression format '{}' does not accept multiple files.",
&formats[0]
))
.detail("The only supported formats that archive files into an archive are .tar and .zip.")
.detail("Formats that bundle files into an archive are .tar and .zip.")
.hint(format!("Try inserting '.tar' or '.zip' before '{}'.", &formats[0]))
.hint(format!("From: {}", output_path))
.hint(format!("To: {}", suggested_output_path));
Expand Down Expand Up @@ -272,7 +266,7 @@ pub fn run(
let not_archives: Vec<PathBuf> = files
.iter()
.zip(&formats)
.filter(|(_, formats)| !formats.get(0).map(Extension::is_archive).unwrap_or(false))
.filter(|(_, formats)| !formats.first().map(Extension::is_archive).unwrap_or(false))
.map(|(path, _)| path.clone())
.collect();

Expand Down
8 changes: 0 additions & 8 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::{
env,
fs::ReadDir,
io::Read,
path::{Path, PathBuf},
};
Expand All @@ -12,13 +11,6 @@ use fs_err as fs;
use super::{to_utf, user_wants_to_overwrite};
use crate::{extension::Extension, info, QuestionPolicy};

/// Checks if given path points to an empty directory.
pub fn dir_is_empty(dir_path: &Path) -> bool {
let is_empty = |mut rd: ReadDir| rd.next().is_none();

dir_path.read_dir().map(is_empty).unwrap_or_default()
}

/// Remove `path` asking the user to overwrite if necessary.
///
/// * `Ok(true)` means the path is clear,
Expand Down
3 changes: 1 addition & 2 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ mod question;
pub use file_visibility::FileVisibilityPolicy;
pub use formatting::{nice_directory_display, pretty_format_list_of_paths, strip_cur_dir, to_utf};
pub use fs::{
cd_into_same_dir_as, clear_path, create_dir_if_non_existent, dir_is_empty, is_symlink, remove_file_or_dir,
try_infer_extension,
cd_into_same_dir_as, clear_path, create_dir_if_non_existent, is_symlink, remove_file_or_dir, try_infer_extension,
};
pub use question::{
ask_to_create_file, user_wants_to_continue, user_wants_to_overwrite, QuestionAction, QuestionPolicy,
Expand Down