summaryrefslogtreecommitdiff
path: root/cli/args/flags.rs
diff options
context:
space:
mode:
authorAsher Gomez <ashersaupingomez@gmail.com>2022-11-24 14:00:31 +1100
committerGitHub <noreply@github.com>2022-11-24 03:00:31 +0000
commitfe7e3a12ca02792215f7598302c42113bcdc4458 (patch)
tree1bea37db723f77c96eec000ece5c738ab6f0eef8 /cli/args/flags.rs
parentbeaa0d88679c96e643f411d04a4ce9f6d159eaeb (diff)
feat(cli): add warning for incorrectly ordered flags (#16734)
This code checks if permission flags are incorrectly defined after the module name (e.g. `deno run mod.ts --allow-read` instead of the correct `deno run --allow-read mod.ts`). If so, a simple warning is displayed.
Diffstat (limited to 'cli/args/flags.rs')
-rw-r--r--cli/args/flags.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/cli/args/flags.rs b/cli/args/flags.rs
index e322aa1e7..829051bce 100644
--- a/cli/args/flags.rs
+++ b/cli/args/flags.rs
@@ -493,6 +493,32 @@ impl Flags {
prompt: !self.no_prompt,
}
}
+
+ pub fn has_permission(&self) -> bool {
+ self.allow_all
+ || self.allow_hrtime
+ || self.allow_env.is_some()
+ || self.allow_ffi.is_some()
+ || self.allow_net.is_some()
+ || self.allow_read.is_some()
+ || self.allow_run.is_some()
+ || self.allow_sys.is_some()
+ || self.allow_write.is_some()
+ }
+
+ pub fn has_permission_in_argv(&self) -> bool {
+ self.argv.iter().any(|arg| {
+ arg == "--allow-all"
+ || arg == "--allow-hrtime"
+ || arg.starts_with("--allow-env")
+ || arg.starts_with("--allow-ffi")
+ || arg.starts_with("--allow-net")
+ || arg.starts_with("--allow-read")
+ || arg.starts_with("--allow-run")
+ || arg.starts_with("--allow-sys")
+ || arg.starts_with("--allow-write")
+ })
+ }
}
static ENV_VARIABLES_HELP: &str = r#"ENVIRONMENT VARIABLES:
@@ -3389,6 +3415,24 @@ mod tests {
}
#[test]
+ fn has_permission() {
+ let r = flags_from_vec(svec!["deno", "run", "--allow-read", "x.ts"]);
+ assert_eq!(r.unwrap().has_permission(), true);
+
+ let r = flags_from_vec(svec!["deno", "run", "x.ts"]);
+ assert_eq!(r.unwrap().has_permission(), false);
+ }
+
+ #[test]
+ fn has_permission_in_argv() {
+ let r = flags_from_vec(svec!["deno", "run", "x.ts", "--allow-read"]);
+ assert_eq!(r.unwrap().has_permission_in_argv(), true);
+
+ let r = flags_from_vec(svec!["deno", "run", "x.ts"]);
+ assert_eq!(r.unwrap().has_permission_in_argv(), false);
+ }
+
+ #[test]
fn script_args() {
let r = flags_from_vec(svec![
"deno",