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

Added support for glob patterns in quotes #536

Merged
merged 8 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 1 addition & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ version = "1.7.0"
optional = true
version = "4.3.8"

[dependencies.wild]
[target.'cfg(windows)'.dependencies.glob]
optional = true
version = "2.1.0"
version = "0.3.1"

[dependencies.image]
optional = true
Expand All @@ -66,7 +66,7 @@ version = "0.24.6"
rustc_version = "0.4.0"

[features]
binary = ["clap", "wild", "stderrlog"]
binary = ["clap", "glob", "stderrlog"]
default = ["binary", "filetime", "parallel", "zopfli"]
parallel = ["rayon", "indexmap/rayon", "crossbeam-channel"]
freestanding = ["libdeflater/freestanding"]
Expand Down
24 changes: 23 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ Heuristic filter selection strategies:
8 => BigEnt Highest Shannon entropy of bigrams
9 => Brute Smallest compressed size (slow)",
)
.get_matches_from(wild::args());
.get_matches();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'll just note that there's a subtle difference to be aware of here: wild::args() corresponds to env::args(), while get_matches() uses env::args_os() (corresponding to wild::args_os()). It affects what happens with paths that contain invalid UTF-8.
Not suggesting to change anything, just pointing this out.


let (out_file, out_dir, opts) = match parse_opts_into_struct(&matches) {
Ok(x) => x,
Expand All @@ -306,6 +306,18 @@ Heuristic filter selection strategies:
};

let files = collect_files(
#[cfg(windows)]
matches
.get_many::<PathBuf>("files")
.unwrap()
.flat_map(|path| {
apply_glob_pattern(path).unwrap_or_else(|e| {
warn!("{path:?}: {e}");
vec![]
})
})
.collect(),
#[cfg(not(windows))]
matches
.get_many::<PathBuf>("files")
.unwrap()
Expand Down Expand Up @@ -383,6 +395,16 @@ fn collect_files(
in_out_pairs
}

#[cfg(windows)]
fn apply_glob_pattern(path: &std::path::Path) -> Result<Vec<PathBuf>, String> {
let input_path = path.to_str().ok_or("Failed to read path.".to_string())?;

Ok(glob::glob(input_path)
.map_err(|e| e.to_string())?
.flatten()
.collect())
}

fn parse_opts_into_struct(
matches: &ArgMatches,
) -> Result<(OutFile, Option<PathBuf>, Options), String> {
Expand Down