From fe7e3a12ca02792215f7598302c42113bcdc4458 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Thu, 24 Nov 2022 14:00:31 +1100 Subject: 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. --- cli/args/flags.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'cli/args/flags.rs') 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: @@ -3388,6 +3414,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![ -- cgit v1.2.3