From df6b5b3e68533bf6ee6b4cacf2ed5ecd2adb571c Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 9 Jun 2019 07:24:12 +1200 Subject: [PATCH] Check url for extension during magic open --- src/commands/open.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/commands/open.rs b/src/commands/open.rs index 056736186..7f3c79ab4 100644 --- a/src/commands/open.rs +++ b/src/commands/open.rs @@ -12,13 +12,25 @@ pub fn open(args: CommandArgs) -> Result { let cwd = args.env.lock().unwrap().cwd().to_path_buf(); let mut full_path = PathBuf::from(cwd); - let contents = match &args.positional[0].item { + let (file_extension, contents) = match &args.positional[0].item { Value::Primitive(Primitive::String(s)) => { if s.starts_with("http:") || s.starts_with("https:") { let response = reqwest::get(s); match response { Ok(mut r) => match r.text() { - Ok(s) => s, + Ok(s) => { + let fname = r + .url() + .path_segments() + .and_then(|segments| segments.last()) + .and_then(|name| if name.is_empty() { None } else { Some(name) }) + .and_then(|name| { + PathBuf::from(name) + .extension() + .map(|name| name.to_string_lossy().to_string()) + }); + (fname, s) + } Err(_) => { return Err(ShellError::labeled_error( "Web page contents corrupt", @@ -38,7 +50,12 @@ pub fn open(args: CommandArgs) -> Result { } else { full_path.push(Path::new(&s)); match std::fs::read_to_string(&full_path) { - Ok(s) => s, + Ok(s) => ( + full_path + .extension() + .map(|name| name.to_string_lossy().to_string()), + s, + ), Err(_) => { return Err(ShellError::labeled_error( "File cound not be opened", @@ -75,7 +92,7 @@ pub fn open(args: CommandArgs) -> Result { _ => false, }; - match full_path.extension() { + match file_extension { Some(x) if x == "toml" && !open_raw => { stream.push_back(ReturnValue::Value( crate::commands::from_toml::from_toml_string_to_value(contents),