-
Notifications
You must be signed in to change notification settings - Fork 624
Exact path matching command line option #980
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
use std::ffi::OsString; | ||
use std::iter::Rev; | ||
use std::ops::Range; | ||
use std::path::{Component, PathBuf}; | ||
use std::{fs, path}; | ||
|
||
use glob::Pattern; | ||
|
@@ -40,6 +42,10 @@ impl<'a> Stream<'a> { | |
continue; | ||
} | ||
|
||
if !self.filter_by_exact(&dir.path) { | ||
continue; | ||
} | ||
|
||
let dir = &self.db.dirs()[idx]; | ||
return Some(dir); | ||
} | ||
|
@@ -91,6 +97,34 @@ impl<'a> Stream<'a> { | |
if self.options.resolve_symlinks { fs::symlink_metadata } else { fs::metadata }; | ||
resolver(path).map(|metadata| metadata.is_dir()).unwrap_or_default() | ||
} | ||
|
||
fn filter_by_exact(&self, path: &str) -> bool { | ||
if !self.options.exact { | ||
return true; | ||
} | ||
|
||
let path = util::to_lowercase(path); | ||
let path = PathBuf::from(path); | ||
let mut components: Vec<Component> = path.components().collect(); | ||
|
||
for keyword in self.options.keywords.iter().rev().map(OsString::from) { | ||
let idx = components.iter().rposition(|component| { | ||
if let Component::Normal(sub_path) = component { | ||
sub_path == &keyword | ||
} else { | ||
false | ||
} | ||
}); | ||
|
||
if let Some(idx) = idx { | ||
components = components.drain(0..idx).collect(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This... is convoluted to say the least. |
||
} else { | ||
return false; | ||
}; | ||
} | ||
|
||
true | ||
} | ||
} | ||
|
||
pub struct StreamOptions { | ||
|
@@ -109,6 +143,9 @@ pub struct StreamOptions { | |
/// Whether to resolve symlinks when checking if a directory exists. | ||
resolve_symlinks: bool, | ||
|
||
// Whether to only allow exact match of directory names. | ||
exact: bool, | ||
|
||
/// Directories that do not exist and haven't been accessed since TTL will | ||
/// be lazily removed. | ||
ttl: Epoch, | ||
|
@@ -122,6 +159,7 @@ impl StreamOptions { | |
exclude: Vec::new(), | ||
exists: false, | ||
resolve_symlinks: false, | ||
exact: false, | ||
ttl: now.saturating_sub(3 * MONTH), | ||
} | ||
} | ||
|
@@ -149,6 +187,11 @@ impl StreamOptions { | |
self.resolve_symlinks = resolve_symlinks; | ||
self | ||
} | ||
|
||
pub fn with_exact(mut self, exact: bool) -> Self { | ||
self.exact = exact; | ||
self | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd spend a bit more time explaining this feature. This is what will show up in
--help