Skip to content

Commit df4f48b

Browse files
feat(cli): add trust and revoke custom language policies
1 parent 2533449 commit df4f48b

6 files changed

Lines changed: 264 additions & 34 deletions

File tree

‎Cargo.lock‎

Lines changed: 52 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/cli/Cargo.toml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ anyhow.workspace = true
3838
clap = { version = "4.5.4", features = ["derive"] }
3939
codespan-reporting = "0.13.0"
4040
crossterm = "0.29.0"
41+
directories = "6.0.0"
4142
ignore.workspace = true
4243
regex.workspace = true
4344
dashmap.workspace = true
@@ -50,18 +51,18 @@ serde_yaml.workspace = true
5051
serde-sarif = "0.8.0"
5152
similar = { version = "3.0.0", features = ["inline"] }
5253
smallvec = "1.13.2"
54+
tempfile = "3.10.1"
5355
tokio = { version = "1.37.0", features = ["rt-multi-thread", "io-std"] }
5456
clap_complete = "4.5.2"
5557
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
5658

5759
[dev-dependencies]
5860
assert_cmd = "2.0.14"
5961
predicates = "3.1.0"
60-
tempfile = "3.10.1"
6162

6263
# Support cargo bininstall, https://github.com/ast-grep/ast-grep/issues/1742
6364
[package.metadata.binstall]
6465
pkg-url = "{ repo }/releases/download/{ version }/app-{ target }{ archive-suffix }"
6566
pkg-fmt = "zip"
6667
bin-dir = "{ bin }{ binary-ext }"
67-
disabled-strategies = ["quick-install"]
68+
disabled-strategies = ["quick-install"]

‎crates/cli/src/config.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ pub enum CustomLanguagePolicy {
2121
/// Skip native custom language libraries without reporting an error.
2222
#[default]
2323
Ignore,
24+
/// Load native custom language libraries and remember this project.
25+
Trust,
26+
/// Forget this project and ignore its native custom language libraries.
27+
Revoke,
2428
}
2529

2630
#[derive(Serialize, Deserialize, Clone)]
@@ -158,6 +162,9 @@ fn register_custom_language(
158162
);
159163
}
160164
CustomLanguagePolicy::Ignore => {}
165+
CustomLanguagePolicy::Trust | CustomLanguagePolicy::Revoke => {
166+
unreachable!("persistent policies must be resolved before project setup")
167+
}
161168
}
162169
}
163170
if let Some(globs) = sg_config.language_globs {
@@ -307,7 +314,9 @@ const CONFIG_FILE_YML: &str = "sgconfig.yml";
307314
const CONFIG_FILE_YAML: &str = "sgconfig.yaml";
308315

309316
/// return None if config file does not exist
310-
fn find_config_path_with_default(config_path: Option<PathBuf>) -> Result<Option<PathBuf>> {
317+
pub(crate) fn find_config_path_with_default(
318+
config_path: Option<PathBuf>,
319+
) -> Result<Option<PathBuf>> {
311320
if config_path.is_some() {
312321
return Ok(config_path);
313322
}

‎crates/cli/src/lib.rs‎

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod outline;
77
mod print;
88
mod run;
99
mod scan;
10+
mod trust;
1011
mod utils;
1112
mod 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
107108
fn 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

Comments
 (0)