Ask parallel permission prompts one by one; add allow-all-edits option
This commit is contained in:
1 parent
6f951ad7f8
commit
49be5673bd
4 files changed
+42
-8
No files matched your search
@@ -77,6 +77,8 @@ pub struct Request {
|
||||
pub enum Decision {
|
||||
Once,
|
||||
Always,
|
||||
/// All write/edit calls for the rest of this session (not persisted).
|
||||
Edits,
|
||||
Deny,
|
||||
}
|
||||
|
||||
@@ -161,6 +163,7 @@ fn glob(pat: &str, s: &str) -> bool {
|
||||
#[derive(Default)]
|
||||
pub struct Allow {
|
||||
rules: HashSet<String>,
|
||||
all_edits: bool,
|
||||
file: Option<PathBuf>,
|
||||
}
|
||||
|
||||
@@ -173,12 +176,16 @@ impl Allow {
|
||||
.unwrap_or_default();
|
||||
Self {
|
||||
rules,
|
||||
all_edits: false,
|
||||
file: Some(file),
|
||||
}
|
||||
}
|
||||
|
||||
/// Is this request covered by a stored rule? `bash:cargo test *` also covers a bare `cargo test`.
|
||||
pub fn allows(&self, r: &Request) -> bool {
|
||||
if self.all_edits && matches!(r.tool.as_str(), "write" | "edit") {
|
||||
return true;
|
||||
}
|
||||
if r.tool != "bash" {
|
||||
return self.rules.contains(&r.rule);
|
||||
}
|
||||
@@ -191,6 +198,11 @@ impl Allow {
|
||||
.any(|p| glob(p, &r.detail) || p.strip_suffix(" *").is_some_and(|b| b == r.detail))
|
||||
}
|
||||
|
||||
/// Allow every write/edit until the session ends.
|
||||
pub fn allow_edits(&mut self) {
|
||||
self.all_edits = true;
|
||||
}
|
||||
|
||||
/// Remember a rule; the file is best effort (the rule holds for this session either way).
|
||||
pub fn add(&mut self, rule: &str) {
|
||||
if rule.is_empty() {
|
||||
@@ -265,5 +277,9 @@ mod tests {
|
||||
assert!(Allow::load(d.path()).allows(&bash("cargo test -q")));
|
||||
a.add("");
|
||||
assert!(!a.allows(&bash("")));
|
||||
let e = request("edit", &json!({"path": "a.rs"}));
|
||||
assert!(!a.allows(&e));
|
||||
a.allow_edits();
|
||||
assert!(a.allows(&e) && !a.allows(&bash("make")));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user