@@ -7,6 +7,7 @@ mod outline;
77mod print;
88mod run;
99mod scan;
10+ mod trust;
1011mod utils;
1112mod verify;
1213
@@ -46,9 +47,9 @@ struct App {
4647 /// Path to ast-grep root config, default is sgconfig.yml.
4748 #[ clap( short, long, global = true , value_name = "CONFIG_FILE" ) ]
4849 config : Option < PathBuf > ,
49- /// Control how sgconfig.yml handles native custom language libraries.
50- #[ clap( long, global = true , value_enum, default_value_t ) ]
51- custom_languages : CustomLanguagePolicy ,
50+ /// Control how sgconfig.yml handles native custom language libraries (default: ignore) .
51+ #[ clap( long, global = true , value_enum) ]
52+ custom_languages : Option < CustomLanguagePolicy > ,
5253}
5354
5455#[ derive( Subcommand ) ]
@@ -106,11 +107,15 @@ fn insert_default_run(args: &mut Vec<String>) {
106107/// finding project and setup custom language configuration
107108fn setup_project_is_possible (
108109 args : & [ String ] ,
109- custom_languages : CustomLanguagePolicy ,
110+ custom_languages : Option < CustomLanguagePolicy > ,
110111) -> Result < Result < ProjectConfig > > {
111112 let mut config = None ;
112113 for i in 0 ..args. len ( ) {
113114 let arg = & args[ i] ;
115+ if let Some ( config_file) = arg. strip_prefix ( "-c" ) . filter ( |path| !path. is_empty ( ) ) {
116+ config = Some ( config_file. strip_prefix ( '=' ) . unwrap_or ( config_file) . into ( ) ) ;
117+ break ;
118+ }
114119 if !is_command ( arg, "config" ) {
115120 continue ;
116121 }
@@ -124,13 +129,13 @@ fn setup_project_is_possible(
124129 if i + 1 >= args. len ( ) || args[ i + 1 ] . starts_with ( '-' ) {
125130 return Err ( anyhow:: anyhow!( "missing config file after -c" ) ) ;
126131 }
127- let config_file = ( & args[ i + 1 ] ) . into ( ) ;
128- config = Some ( config_file) ;
132+ config = Some ( ( & args[ i + 1 ] ) . into ( ) ) ;
129133 }
130- ProjectConfig :: setup ( config, custom_languages)
134+ let policy = trust:: resolve_policy ( custom_languages, config. as_deref ( ) ) ?;
135+ ProjectConfig :: setup ( config, policy)
131136}
132137
133- fn parse_custom_language_policy ( args : & [ String ] ) -> CustomLanguagePolicy {
138+ fn parse_custom_language_policy ( args : & [ String ] ) -> Option < CustomLanguagePolicy > {
134139 let mut policy = None ;
135140 let mut i = 1 ;
136141 while i < args. len ( ) {
@@ -139,24 +144,35 @@ fn parse_custom_language_policy(args: &[String]) -> CustomLanguagePolicy {
139144 break ;
140145 }
141146 let value = if arg == "--custom-languages" {
147+ if policy. is_some ( ) {
148+ return Some ( CustomLanguagePolicy :: Ignore ) ;
149+ }
150+ // Safe fallback while Clap handles a missing or invalid value.
151+ policy = Some ( CustomLanguagePolicy :: Ignore ) ;
142152 i += 1 ;
143153 args. get ( i) . map ( String :: as_str)
144154 } else {
145- arg. strip_prefix ( "--custom-languages=" )
155+ let value = arg. strip_prefix ( "--custom-languages=" ) ;
156+ if value. is_some ( ) {
157+ if policy. is_some ( ) {
158+ return Some ( CustomLanguagePolicy :: Ignore ) ;
159+ }
160+ policy = Some ( CustomLanguagePolicy :: Ignore ) ;
161+ }
162+ value
146163 } ;
147164 if let Some ( value) = value {
148- if policy. is_some ( ) {
149- return CustomLanguagePolicy :: Ignore ;
150- }
151165 policy = match value {
152166 "allow" => Some ( CustomLanguagePolicy :: Allow ) ,
153167 "ignore" => Some ( CustomLanguagePolicy :: Ignore ) ,
154- _ => return CustomLanguagePolicy :: Ignore ,
168+ "trust" => Some ( CustomLanguagePolicy :: Trust ) ,
169+ "revoke" => Some ( CustomLanguagePolicy :: Revoke ) ,
170+ _ => Some ( CustomLanguagePolicy :: Ignore ) ,
155171 } ;
156172 }
157173 i += 1 ;
158174 }
159- policy. unwrap_or_default ( )
175+ policy
160176}
161177
162178// this wrapper function is for testing
@@ -231,25 +247,33 @@ mod test_cli {
231247 assert_eq ! ( args[ 1 ] , "run" ) ;
232248 }
233249
234- #[ test]
235- fn test_custom_language_policy ( ) {
236- let args = |args : & [ & str ] | {
237- args
250+ fn parse_policy ( args : & [ & str ] ) -> Option < CustomLanguagePolicy > {
251+ parse_custom_language_policy (
252+ & args
238253 . iter ( )
239254 . map ( |arg| ( * arg) . to_string ( ) )
240- . collect :: < Vec < _ > > ( )
241- } ;
255+ . collect :: < Vec < _ > > ( ) ,
256+ )
257+ }
258+
259+ #[ test]
260+ fn test_custom_language_policy ( ) {
261+ assert_eq ! ( parse_policy( & [ "sg" , "scan" ] ) , None ) ;
262+ assert_eq ! (
263+ parse_policy( & [ "sg" , "scan" , "--custom-languages=allow" ] ) ,
264+ Some ( CustomLanguagePolicy :: Allow ) ,
265+ ) ;
242266 assert_eq ! (
243- parse_custom_language_policy ( & args ( & [ "sg" , "scan" ] ) ) ,
244- CustomLanguagePolicy :: Ignore
267+ parse_policy ( & [ "sg" , "run" , "--" , "--custom-languages" , "allow" ] ) ,
268+ None ,
245269 ) ;
246270 assert_eq ! (
247- parse_custom_language_policy ( & args ( & [ "sg" , "scan" , "--custom-languages=allow" ] ) ) ,
248- CustomLanguagePolicy :: Allow
271+ parse_policy ( & [ "sg" , "scan" , "--custom-languages" , "trust" ] ) ,
272+ Some ( CustomLanguagePolicy :: Trust ) ,
249273 ) ;
250274 assert_eq ! (
251- parse_custom_language_policy ( & args ( & [ "sg" , "run " , "--" , "-- custom-languages" , "allow" ] ) ) ,
252- CustomLanguagePolicy :: Ignore
275+ parse_policy ( & [ "sg" , "scan " , "--custom-languages=revoke" ] ) ,
276+ Some ( CustomLanguagePolicy :: Revoke ) ,
253277 ) ;
254278 }
255279
@@ -321,6 +345,8 @@ mod test_cli {
321345 ok ( "scan" ) ;
322346 ok ( "scan --custom-languages allow" ) ;
323347 ok ( "scan --custom-languages ignore" ) ;
348+ ok ( "scan --custom-languages trust" ) ;
349+ ok ( "scan --custom-languages revoke" ) ;
324350 error ( "scan --custom-languages deny" ) ;
325351 error ( "scan --custom-languages invalid" ) ;
326352 ok ( "scan dir" ) ;
0 commit comments