Skip to content

Commit

Permalink
feat: --prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Mar 18, 2021
1 parent 0e70d9e commit 0849cdc
Show file tree
Hide file tree
Showing 19 changed files with 451 additions and 310 deletions.
56 changes: 28 additions & 28 deletions cli/file_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ impl FileFetcher {
fn fetch_remote(
&self,
specifier: &ModuleSpecifier,
permissions: &Permissions,
permissions: &mut Permissions,
redirect_limit: i64,
) -> Pin<Box<dyn Future<Output = Result<File, AnyError>> + Send>> {
debug!("FileFetcher::fetch_remote() - specifier: {}", specifier);
Expand Down Expand Up @@ -497,7 +497,7 @@ impl FileFetcher {
};
let maybe_auth_token = self.auth_tokens.get(&specifier);
let specifier = specifier.clone();
let permissions = permissions.clone();
let mut permissions = permissions.clone();
let client = self.http_client.clone();
let file_fetcher = self.clone();
// A single pass of fetch either yields code or yields a redirect.
Expand All @@ -517,7 +517,7 @@ impl FileFetcher {
FetchOnceResult::Redirect(redirect_url, headers) => {
file_fetcher.http_cache.set(&specifier, headers, &[])?;
file_fetcher
.fetch_remote(&redirect_url, &permissions, redirect_limit - 1)
.fetch_remote(&redirect_url, &mut permissions, redirect_limit - 1)
.await
}
FetchOnceResult::Code(bytes, headers) => {
Expand All @@ -537,7 +537,7 @@ impl FileFetcher {
pub async fn fetch(
&self,
specifier: &ModuleSpecifier,
permissions: &Permissions,
permissions: &mut Permissions,
) -> Result<File, AnyError> {
debug!("FileFetcher::fetch() - specifier: {}", specifier);
let scheme = get_validated_scheme(specifier)?;
Expand Down Expand Up @@ -633,7 +633,7 @@ mod tests {
async fn test_fetch(specifier: &ModuleSpecifier) -> (File, FileFetcher) {
let (file_fetcher, _) = setup(CacheSetting::ReloadAll, None);
let result = file_fetcher
.fetch(specifier, &Permissions::allow_all())
.fetch(specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());
(result.unwrap(), file_fetcher)
Expand All @@ -645,7 +645,7 @@ mod tests {
let _http_server_guard = test_util::http_server();
let (file_fetcher, _) = setup(CacheSetting::ReloadAll, None);
let result: Result<File, AnyError> = file_fetcher
.fetch_remote(specifier, &Permissions::allow_all(), 1)
.fetch_remote(specifier, &mut Permissions::allow_all(), 1)
.await;
assert!(result.is_ok());
let (_, headers) = file_fetcher.http_cache.get(specifier).unwrap();
Expand Down Expand Up @@ -926,7 +926,7 @@ mod tests {
file_fetcher.insert_cached(file.clone());

let result = file_fetcher
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());
let result_file = result.unwrap();
Expand All @@ -943,7 +943,7 @@ mod tests {
.unwrap();

let result = file_fetcher
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());

Expand Down Expand Up @@ -974,7 +974,7 @@ mod tests {
let specifier = resolve_url("data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=").unwrap();

let result = file_fetcher
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());
let file = result.unwrap();
Expand All @@ -998,7 +998,7 @@ mod tests {
.unwrap();

let result = file_fetcher
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());
let file = result.unwrap();
Expand All @@ -1021,7 +1021,7 @@ mod tests {
metadata.write(&cache_filename).unwrap();

let result = file_fetcher_01
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());
let file = result.unwrap();
Expand All @@ -1042,7 +1042,7 @@ mod tests {
metadata.write(&cache_filename).unwrap();

let result = file_fetcher_02
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());
let file = result.unwrap();
Expand All @@ -1063,7 +1063,7 @@ mod tests {
)
.expect("setup failed");
let result = file_fetcher
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());
let file = result.unwrap();
Expand Down Expand Up @@ -1097,7 +1097,7 @@ mod tests {
.unwrap();

let result = file_fetcher_01
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());

Expand All @@ -1116,7 +1116,7 @@ mod tests {
)
.expect("could not create file fetcher");
let result = file_fetcher_02
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());

Expand Down Expand Up @@ -1155,7 +1155,7 @@ mod tests {
.unwrap();

let result = file_fetcher
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());
let file = result.unwrap();
Expand Down Expand Up @@ -1216,7 +1216,7 @@ mod tests {
.unwrap();

let result = file_fetcher
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());
let file = result.unwrap();
Expand Down Expand Up @@ -1287,7 +1287,7 @@ mod tests {
.unwrap();

let result = file_fetcher_01
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());

Expand All @@ -1306,7 +1306,7 @@ mod tests {
)
.expect("could not create file fetcher");
let result = file_fetcher_02
.fetch(&redirected_specifier, &Permissions::allow_all())
.fetch(&redirected_specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());

Expand All @@ -1333,12 +1333,12 @@ mod tests {
.unwrap();

let result = file_fetcher
.fetch_remote(&specifier, &Permissions::allow_all(), 2)
.fetch_remote(&specifier, &mut Permissions::allow_all(), 2)
.await;
assert!(result.is_ok());

let result = file_fetcher
.fetch_remote(&specifier, &Permissions::allow_all(), 1)
.fetch_remote(&specifier, &mut Permissions::allow_all(), 1)
.await;
assert!(result.is_err());

Expand Down Expand Up @@ -1371,7 +1371,7 @@ mod tests {
.unwrap();

let result = file_fetcher
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());
let file = result.unwrap();
Expand Down Expand Up @@ -1418,7 +1418,7 @@ mod tests {
resolve_url("http:https://localhost:4545/cli/tests/002_hello.ts").unwrap();

let result = file_fetcher
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_err());
let err = result.unwrap_err();
Expand Down Expand Up @@ -1451,20 +1451,20 @@ mod tests {
resolve_url("http:https://localhost:4545/cli/tests/002_hello.ts").unwrap();

let result = file_fetcher_01
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_err());
let err = result.unwrap_err();
assert_eq!(get_custom_error_class(&err), Some("NotFound"));
assert_eq!(err.to_string(), "Specifier not found in cache: \"http:https://localhost:4545/cli/tests/002_hello.ts\", --cached-only is specified.");

let result = file_fetcher_02
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());

let result = file_fetcher_01
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());

Expand All @@ -1482,7 +1482,7 @@ mod tests {
fs::write(fixture_path.clone(), r#"console.log("hello deno");"#)
.expect("could not write file");
let result = file_fetcher
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());
let file = result.unwrap();
Expand All @@ -1491,7 +1491,7 @@ mod tests {
fs::write(fixture_path, r#"console.log("goodbye deno");"#)
.expect("could not write file");
let result = file_fetcher
.fetch(&specifier, &Permissions::allow_all())
.fetch(&specifier, &mut Permissions::allow_all())
.await;
assert!(result.is_ok());
let file = result.unwrap();
Expand Down
11 changes: 10 additions & 1 deletion cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub struct Flags {
pub lock_write: bool,
pub log_level: Option<Level>,
pub no_check: bool,
pub no_prompts: bool,
pub prompt: bool,
pub no_remote: bool,
pub reload: bool,
pub repl: bool,
Expand Down Expand Up @@ -221,6 +221,7 @@ impl From<Flags> for PermissionsOptions {
allow_read: flags.allow_read,
allow_run: flags.allow_run,
allow_write: flags.allow_write,
prompt: flags.prompt,
}
}
}
Expand Down Expand Up @@ -1416,6 +1417,11 @@ fn permission_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
.long("allow-all")
.help("Allow all permissions"),
)
.arg(
Arg::with_name("prompt")
.long("prompt")
.help("Fallback to prompt if required permission wasn't passed"),
)
}

fn run_subcommand<'a, 'b>() -> App<'a, 'b> {
Expand Down Expand Up @@ -1829,6 +1835,9 @@ fn permission_args_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
flags.allow_plugin = true;
flags.allow_hrtime = true;
}
if matches.is_present("prompt") {
flags.prompt = true;
}
}

// TODO(ry) move this to utility module and add test.
Expand Down
4 changes: 2 additions & 2 deletions cli/ops/runtime_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async fn op_emit(
let args: EmitArgs = serde_json::from_value(args)?;
let root_specifier = args.root_specifier;
let program_state = state.borrow().borrow::<Arc<ProgramState>>().clone();
let runtime_permissions = {
let mut runtime_permissions = {
let state = state.borrow();
state.borrow::<Permissions>().clone()
};
Expand All @@ -86,7 +86,7 @@ async fn op_emit(
} else {
let file = program_state
.file_fetcher
.fetch(&import_map_specifier, &runtime_permissions)
.fetch(&import_map_specifier, &mut runtime_permissions)
.await?;
ImportMap::from_json(import_map_specifier.as_str(), &file.source)?
};
Expand Down
4 changes: 2 additions & 2 deletions cli/program_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl ProgramState {
format!("Bad URL (\"{}\") for import map.", import_map_url),
)?;
let file = file_fetcher
.fetch(&import_map_specifier, &Permissions::allow_all())
.fetch(&import_map_specifier, &mut Permissions::allow_all())
.await?;
let import_map =
ImportMap::from_json(import_map_specifier.as_str(), &file.source)?;
Expand Down Expand Up @@ -145,7 +145,7 @@ impl ProgramState {
self: &Arc<Self>,
specifier: ModuleSpecifier,
lib: TypeLib,
runtime_permissions: Permissions,
mut runtime_permissions: Permissions,
is_dynamic: bool,
maybe_import_map: Option<ImportMap>,
) -> Result<(), AnyError> {
Expand Down
4 changes: 2 additions & 2 deletions cli/specifier_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl SpecifierHandler for FetchHandler {
// When the module graph fetches dynamic modules, the set of dynamic
// permissions need to be applied. Other static imports have all
// permissions.
let permissions = if is_dynamic {
let mut permissions = if is_dynamic {
self.runtime_permissions.clone()
} else {
Permissions::allow_all()
Expand All @@ -266,7 +266,7 @@ impl SpecifierHandler for FetchHandler {

async move {
let source_file = file_fetcher
.fetch(&requested_specifier, &permissions)
.fetch(&requested_specifier, &mut permissions)
.await
.map_err(|err| {
let err = if let Some(e) = err.downcast_ref::<std::io::Error>() {
Expand Down
2 changes: 1 addition & 1 deletion cli/tools/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub fn compile_to_runtime_flags(
lock_write: false,
log_level: flags.log_level,
no_check: false,
no_prompts: flags.no_prompts,
prompt: flags.prompt,
no_remote: false,
reload: false,
repl: false,
Expand Down
12 changes: 6 additions & 6 deletions op_crates/fetch/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ pub fn init(isolate: &mut JsRuntime) {
}

pub trait FetchPermissions {
fn check_net_url(&self, _url: &Url) -> Result<(), AnyError>;
fn check_read(&self, _p: &Path) -> Result<(), AnyError>;
fn check_net_url(&mut self, _url: &Url) -> Result<(), AnyError>;
fn check_read(&mut self, _p: &Path) -> Result<(), AnyError>;
}

/// For use with `op_fetch` when the user does not want permissions.
pub struct NoFetchPermissions;

impl FetchPermissions for NoFetchPermissions {
fn check_net_url(&self, _url: &Url) -> Result<(), AnyError> {
fn check_net_url(&mut self, _url: &Url) -> Result<(), AnyError> {
Ok(())
}

fn check_read(&self, _p: &Path) -> Result<(), AnyError> {
fn check_read(&mut self, _p: &Path) -> Result<(), AnyError> {
Ok(())
}
}
Expand Down Expand Up @@ -151,7 +151,7 @@ where
return Err(type_error(format!("scheme '{}' not supported", scheme)));
}

let permissions = state.borrow::<FP>();
let permissions = state.borrow_mut::<FP>();
permissions.check_net_url(&url)?;

let mut request = client.request(method, url);
Expand Down Expand Up @@ -395,7 +395,7 @@ where
FP: FetchPermissions + 'static,
{
if let Some(ca_file) = args.ca_file.clone() {
let permissions = state.borrow::<FP>();
let permissions = state.borrow_mut::<FP>();
permissions.check_read(&PathBuf::from(ca_file))?;
}

Expand Down
Loading

0 comments on commit 0849cdc

Please sign in to comment.