Merge pull request #190 from jonathandturner/improved_open_url

Improved open url
This commit is contained in:
Jonathan Turner 2019-07-18 07:25:02 +12:00 committed by GitHub
commit f10225e26b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 54 deletions

View file

@ -67,41 +67,69 @@ pub fn fetch(
if location.starts_with("http:") || location.starts_with("https:") {
let response = reqwest::get(location);
match response {
Ok(mut r) => match r.text() {
Ok(s) => {
let path_extension = 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())
});
let extension = match r.headers().get("content-type") {
Some(content_type) => {
let content_type =
Mime::from_str(content_type.to_str().unwrap()).unwrap();
match (content_type.type_(), content_type.subtype()) {
(mime::APPLICATION, mime::XML) => Some("xml".to_string()),
(mime::APPLICATION, mime::JSON) => Some("json".to_string()),
_ => path_extension,
}
Ok(mut r) => match r.headers().get("content-type") {
Some(content_type) => {
let content_type = Mime::from_str(content_type.to_str().unwrap()).unwrap();
match (content_type.type_(), content_type.subtype()) {
(mime::APPLICATION, mime::XML) => Ok((
Some("xml".to_string()),
Value::string(r.text().unwrap()),
span,
)),
(mime::APPLICATION, mime::JSON) => Ok((
Some("json".to_string()),
Value::string(r.text().unwrap()),
span,
)),
(mime::APPLICATION, mime::OCTET_STREAM) => {
let mut buf: Vec<u8> = vec![];
r.copy_to(&mut buf).map_err(|_| {
ShellError::labeled_error(
"Could not load binary file",
"could not load",
span,
)
})?;
Ok((None, Value::Binary(buf), span))
}
None => path_extension,
};
(mime::IMAGE, image_ty) => {
let mut buf: Vec<u8> = vec![];
r.copy_to(&mut buf).map_err(|_| {
ShellError::labeled_error(
"Could not load image file",
"could not load",
span,
)
})?;
Ok((Some(image_ty.to_string()), Value::Binary(buf), span))
}
(mime::TEXT, mime::HTML) => Ok((
Some("html".to_string()),
Value::string(r.text().unwrap()),
span,
)),
(mime::TEXT, mime::PLAIN) => {
let path_extension = 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())
});
Ok((extension, Value::string(s), span))
}
Err(_) => {
return Err(ShellError::labeled_error(
"Web page contents corrupt",
"received garbled data",
span,
));
Ok((path_extension, Value::string(r.text().unwrap()), span))
}
(ty, sub_ty) => Ok((
None,
Value::string(format!("Not yet support MIME type: {} {}", ty, sub_ty)),
span,
)),
}
}
None => Ok((None, Value::string(format!("No content type found")), span)),
},
Err(_) => {
return Err(ShellError::labeled_error(

View file

@ -7,19 +7,21 @@ macro_rules! nu {
use std::io::prelude::*;
use std::process::{Command, Stdio};
let commands = &*format!("
let commands = &*format!(
"
cd {}
{}
exit",
$cwd,
$commands);
$cwd, $commands
);
let process = match Command::new(helpers::executable_path())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn() {
Ok(child) => child,
Err(why) => panic!("Can't run test {}", why.description()),
.spawn()
{
Ok(child) => child,
Err(why) => panic!("Can't run test {}", why.description()),
};
match process.stdin.unwrap().write_all(commands.as_bytes()) {
@ -44,16 +46,16 @@ macro_rules! nu {
#[macro_export]
macro_rules! nu_error {
($out:ident, $cwd:expr, $commands:expr) => {
use std::error::Error;
use std::io::prelude::*;
use std::process::{Command, Stdio};
let commands = &*format!("
let commands = &*format!(
"
cd {}
{}
exit",
$cwd,
$commands);
$cwd, $commands
);
let mut process = Command::new(helpers::executable_path())
.stdin(Stdio::piped())
@ -62,20 +64,23 @@ macro_rules! nu_error {
.expect("couldn't run test");
let stdin = process.stdin.as_mut().expect("couldn't open stdin");
stdin.write_all(commands.as_bytes()).expect("couldn't write to stdin");
stdin
.write_all(commands.as_bytes())
.expect("couldn't write to stdin");
let output = process.wait_with_output().expect("couldn't read from stderr");
let output = process
.wait_with_output()
.expect("couldn't read from stderr");
let $out = String::from_utf8_lossy(&output.stderr);
};
}
pub fn executable_path() -> PathBuf {
let mut buf = PathBuf::new();
buf.push("target");
buf.push("debug");
buf.push("nu");
buf
buf.push("target");
buf.push("debug");
buf.push("nu");
buf
}
pub fn in_directory(str: &str) -> &str {