1+ use globset:: { Glob , GlobSet , GlobSetBuilder } ;
12use log:: warn;
23use std:: { path:: PathBuf , sync:: Arc } ;
34
@@ -7,29 +8,43 @@ use crate::{fields_value, ops::sdk::*};
78pub struct Spec {
89 path : String ,
910 binary : bool ,
11+ included_patterns : Option < Vec < String > > ,
12+ excluded_patterns : Option < Vec < String > > ,
1013}
1114
1215struct Executor {
1316 root_path_str : String ,
1417 root_path : PathBuf ,
1518 binary : bool ,
19+ included_glob_set : Option < GlobSet > ,
20+ excluded_glob_set : Option < GlobSet > ,
1621}
1722
1823impl Executor {
1924 async fn traverse_dir ( & self , dir_path : & PathBuf , result : & mut Vec < KeyValue > ) -> Result < ( ) > {
2025 for entry in std:: fs:: read_dir ( dir_path) ? {
2126 let entry = entry?;
2227 let path = entry. path ( ) ;
23- if path. is_dir ( ) {
24- Box :: pin ( self . traverse_dir ( & path, result) ) . await ?;
25- } else {
26- if let Some ( file_name) = path. to_str ( ) {
27- result. push ( KeyValue :: Str ( Arc :: from (
28- & file_name[ self . root_path_str . len ( ) + 1 ..] ,
29- ) ) ) ;
30- } else {
31- warn ! ( "Skipped ill-formed file path: {}" , path. display( ) ) ;
28+ if let Some ( file_name) = path. to_str ( ) {
29+ let relative_path = & file_name[ self . root_path_str . len ( ) + 1 ..] ;
30+ if self
31+ . excluded_glob_set
32+ . as_ref ( )
33+ . map_or ( false , |glob_set| glob_set. is_match ( relative_path) )
34+ {
35+ continue ;
3236 }
37+ if path. is_dir ( ) {
38+ Box :: pin ( self . traverse_dir ( & path, result) ) . await ?;
39+ } else if self
40+ . included_glob_set
41+ . as_ref ( )
42+ . map_or ( true , |glob_set| glob_set. is_match ( relative_path) )
43+ {
44+ result. push ( KeyValue :: Str ( Arc :: from ( relative_path) ) ) ;
45+ }
46+ } else {
47+ warn ! ( "Skipped ill-formed file path: {}" , path. display( ) ) ;
3348 }
3449 }
3550 Ok ( ( ) )
@@ -102,6 +117,16 @@ impl SourceFactoryBase for Factory {
102117 root_path_str : spec. path . clone ( ) ,
103118 root_path : PathBuf :: from ( spec. path ) ,
104119 binary : spec. binary ,
120+ included_glob_set : spec. included_patterns . map ( build_glob_set) . transpose ( ) ?,
121+ excluded_glob_set : spec. excluded_patterns . map ( build_glob_set) . transpose ( ) ?,
105122 } ) )
106123 }
107124}
125+
126+ fn build_glob_set ( patterns : Vec < String > ) -> Result < GlobSet > {
127+ let mut builder = GlobSetBuilder :: new ( ) ;
128+ for pattern in patterns {
129+ builder. add ( Glob :: new ( pattern. as_str ( ) ) ?) ;
130+ }
131+ Ok ( builder. build ( ) ?)
132+ }
0 commit comments