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
Changes from 1 commit
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
20 changes: 8 additions & 12 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,24 @@ 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)
));

return Err(error.into());
}
.hint(format!(" ouch compress <FILES>... {output_path} --format tar.gz"))
})?;

let is_some_input_a_folder = files.iter().any(|path| path.is_dir());
let is_multiple_inputs = files.len() > 1;

// If first format is not archive, can't compress folder, or multiple files
// Index safety: empty formats should be checked above.
if !formats[0].is_archive() && (is_some_input_a_folder || is_multiple_inputs) {
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
Expand Down