From f554e3c5f0212c72bdaef976274e4aae28788dac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Tue, 11 Oct 2022 17:33:46 -0300 Subject: [PATCH] warn user if file extension is passed as file name example: .tar.gz is a file named .tar with extension .gz this can be confusing because people might expect .tar.gz to be a .tar.gz archive, but it's currently not --- src/cli.rs | 1 - src/extension.rs | 22 ++++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index e9366fa3a..ee25e4102 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -34,7 +34,6 @@ impl Opts { (true, true) => unreachable!(), }; - // TODO: change this to be just a single function call? let file_visibility_policy = FileVisibilityPolicy::new() .read_git_exclude(opts.gitignore) .read_ignore(opts.gitignore) diff --git a/src/extension.rs b/src/extension.rs index 006f73a69..3aae49024 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -3,6 +3,7 @@ use std::{ffi::OsStr, fmt, path::Path}; use self::CompressionFormat::*; +use crate::warning; /// A wrapper around `CompressionFormat` that allows combinations like `tgz` #[derive(Debug, Clone, Eq)] @@ -103,19 +104,20 @@ pub const SUPPORTED_EXTENSIONS: &[&str] = &[ "zst", ]; -/// Extracts extensions from a path, -/// return both the remaining path and the list of extension objects +/// Extracts extensions from a path. +/// +/// Returns both the remaining path and the list of extension objects pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec) { - // // TODO: check for file names with the name of an extension - // // TODO2: warn the user that currently .tar.gz is a .gz file named .tar - // - // let all = ["tar", "zip", "bz", "bz2", "gz", "xz", "lzma", "lz"]; - // if path.file_name().is_some() && all.iter().any(|ext| path.file_name().unwrap() == *ext) { - // todo!("we found a extension in the path name instead, what to do with this???"); - // } - let mut extensions = vec![]; + if let Some(file_stem) = path.file_stem().and_then(OsStr::to_str) { + let file_stem = file_stem.trim_matches('.'); + + if SUPPORTED_EXTENSIONS.contains(&file_stem) { + warning!("Received a file with name '{file_stem}', but {file_stem} was expected as the extension."); + } + } + // While there is known extensions at the tail, grab them while let Some(extension) = path.extension().and_then(OsStr::to_str) { let formats: &[CompressionFormat] = match extension {