-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We can now find files via glob matching. ``` squawk 'migrations/*.sql' ```
- Loading branch information
Showing
10 changed files
with
134 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::path::PathBuf; | ||
|
||
use log::info; | ||
|
||
#[derive(Debug)] | ||
pub enum FindFilesError { | ||
PatternError(glob::PatternError), | ||
GlobError(glob::GlobError), | ||
} | ||
|
||
impl std::fmt::Display for FindFilesError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
match *self { | ||
Self::PatternError(ref err) => { | ||
write!(f, "Failed to build pattern: {err}") | ||
} | ||
Self::GlobError(ref err) => { | ||
write!(f, "Failed to read file: {err}") | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl std::convert::From<glob::PatternError> for FindFilesError { | ||
fn from(e: glob::PatternError) -> Self { | ||
Self::PatternError(e) | ||
} | ||
} | ||
impl std::convert::From<glob::GlobError> for FindFilesError { | ||
fn from(e: glob::GlobError) -> Self { | ||
Self::GlobError(e) | ||
} | ||
} | ||
|
||
/// Given a list of patterns or paths, along with exclusion patterns, find matching files. | ||
pub fn find_paths( | ||
path_patterns: &[String], | ||
exclude_patterns: &[String], | ||
) -> Result<Vec<PathBuf>, FindFilesError> { | ||
let mut matched_paths = vec![]; | ||
let exclude_paths: Vec<_> = exclude_patterns | ||
.iter() | ||
.map(|x| glob::Pattern::new(x)) | ||
.collect::<Result<_, _>>()?; | ||
for path in path_patterns | ||
.iter() | ||
.flat_map(|x| glob::glob(x)) | ||
.flatten() | ||
.collect::<Result<Vec<_>, _>>()? | ||
{ | ||
if path.is_dir() { | ||
info!("skipping directory path: {}", path.display()); | ||
continue; | ||
} | ||
if let Some(pattern) = exclude_paths | ||
.iter() | ||
.find(|&excluded| excluded.matches(path.to_str().unwrap())) | ||
{ | ||
info!( | ||
"skipping excluded file path: {}. pattern: {}", | ||
path.display(), | ||
pattern | ||
); | ||
|
||
continue; | ||
} | ||
matched_paths.push(path); | ||
} | ||
Ok(matched_paths) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters