mirror of
https://github.com/nushell/nushell.git
synced 2024-12-22 15:13:01 +01:00
Improve open URL. Format and remove warning in tests
This commit is contained in:
parent
73b978ad51
commit
00b3106f05
@ -67,41 +67,53 @@ pub fn fetch(
|
|||||||
if location.starts_with("http:") || location.starts_with("https:") {
|
if location.starts_with("http:") || location.starts_with("https:") {
|
||||||
let response = reqwest::get(location);
|
let response = reqwest::get(location);
|
||||||
match response {
|
match response {
|
||||||
Ok(mut r) => match r.text() {
|
Ok(mut r) => match r.headers().get("content-type") {
|
||||||
Ok(s) => {
|
Some(content_type) => {
|
||||||
let path_extension = r
|
let content_type = Mime::from_str(content_type.to_str().unwrap()).unwrap();
|
||||||
.url()
|
match (content_type.type_(), content_type.subtype()) {
|
||||||
.path_segments()
|
(mime::APPLICATION, mime::XML) => Ok((
|
||||||
.and_then(|segments| segments.last())
|
Some("xml".to_string()),
|
||||||
.and_then(|name| if name.is_empty() { None } else { Some(name) })
|
Value::string(r.text().unwrap()),
|
||||||
.and_then(|name| {
|
span,
|
||||||
PathBuf::from(name)
|
)),
|
||||||
.extension()
|
(mime::APPLICATION, mime::JSON) => Ok((
|
||||||
.map(|name| name.to_string_lossy().to_string())
|
Some("json".to_string()),
|
||||||
});
|
Value::string(r.text().unwrap()),
|
||||||
|
span,
|
||||||
let extension = match r.headers().get("content-type") {
|
)),
|
||||||
Some(content_type) => {
|
(mime::IMAGE, image_ty) => {
|
||||||
let content_type =
|
let mut buf: Vec<u8> = vec![];
|
||||||
Mime::from_str(content_type.to_str().unwrap()).unwrap();
|
r.copy_to(&mut buf).map_err(|_| {
|
||||||
match (content_type.type_(), content_type.subtype()) {
|
ShellError::labeled_error(
|
||||||
(mime::APPLICATION, mime::XML) => Some("xml".to_string()),
|
"Could not load image file",
|
||||||
(mime::APPLICATION, mime::JSON) => Some("json".to_string()),
|
"could not load",
|
||||||
_ => path_extension,
|
span,
|
||||||
}
|
)
|
||||||
|
})?;
|
||||||
|
Ok((Some(image_ty.to_string()), Value::Binary(buf), span))
|
||||||
}
|
}
|
||||||
None => path_extension,
|
(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))
|
Ok((path_extension, Value::string(r.text().unwrap()), span))
|
||||||
}
|
}
|
||||||
Err(_) => {
|
(ty, sub_ty) => Ok((
|
||||||
return Err(ShellError::labeled_error(
|
None,
|
||||||
"Web page contents corrupt",
|
Value::string(format!("Not yet support MIME type: {} {}", ty, sub_ty)),
|
||||||
"received garbled data",
|
span,
|
||||||
span,
|
)),
|
||||||
));
|
}
|
||||||
}
|
}
|
||||||
|
None => Ok((None, Value::string(format!("No content type found")), span)),
|
||||||
},
|
},
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
return Err(ShellError::labeled_error(
|
return Err(ShellError::labeled_error(
|
||||||
|
@ -1,25 +1,27 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! nu {
|
macro_rules! nu {
|
||||||
($out:ident, $cwd:expr, $commands:expr) => {
|
($out:ident, $cwd:expr, $commands:expr) => {
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
|
|
||||||
let commands = &*format!("
|
let commands = &*format!(
|
||||||
|
"
|
||||||
cd {}
|
cd {}
|
||||||
{}
|
{}
|
||||||
exit",
|
exit",
|
||||||
$cwd,
|
$cwd, $commands
|
||||||
$commands);
|
);
|
||||||
|
|
||||||
let process = match Command::new(helpers::executable_path())
|
let process = match Command::new(helpers::executable_path())
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.spawn() {
|
.spawn()
|
||||||
Ok(child) => child,
|
{
|
||||||
Err(why) => panic!("Can't run test {}", why.description()),
|
Ok(child) => child,
|
||||||
|
Err(why) => panic!("Can't run test {}", why.description()),
|
||||||
};
|
};
|
||||||
|
|
||||||
match process.stdin.unwrap().write_all(commands.as_bytes()) {
|
match process.stdin.unwrap().write_all(commands.as_bytes()) {
|
||||||
@ -31,7 +33,7 @@ macro_rules! nu {
|
|||||||
|
|
||||||
match process.stdout.unwrap().read_to_string(&mut _s) {
|
match process.stdout.unwrap().read_to_string(&mut _s) {
|
||||||
Err(why) => panic!("couldn't read stdout: {}", why.description()),
|
Err(why) => panic!("couldn't read stdout: {}", why.description()),
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let _s = _s.replace("\r\n", "\n");
|
let _s = _s.replace("\r\n", "\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -41,19 +43,19 @@ macro_rules! nu {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! nu_error {
|
macro_rules! nu_error {
|
||||||
($out:ident, $cwd:expr, $commands:expr) => {
|
($out:ident, $cwd:expr, $commands:expr) => {
|
||||||
use std::error::Error;
|
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
|
|
||||||
let commands = &*format!("
|
let commands = &*format!(
|
||||||
|
"
|
||||||
cd {}
|
cd {}
|
||||||
{}
|
{}
|
||||||
exit",
|
exit",
|
||||||
$cwd,
|
$cwd, $commands
|
||||||
$commands);
|
);
|
||||||
|
|
||||||
let mut process = Command::new(helpers::executable_path())
|
let mut process = Command::new(helpers::executable_path())
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
@ -62,20 +64,23 @@ macro_rules! nu_error {
|
|||||||
.expect("couldn't run test");
|
.expect("couldn't run test");
|
||||||
|
|
||||||
let stdin = process.stdin.as_mut().expect("couldn't open stdin");
|
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
|
||||||
let output = process.wait_with_output().expect("couldn't read from stderr");
|
.wait_with_output()
|
||||||
|
.expect("couldn't read from stderr");
|
||||||
let $out = String::from_utf8_lossy(&output.stderr);
|
let $out = String::from_utf8_lossy(&output.stderr);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn executable_path() -> PathBuf {
|
pub fn executable_path() -> PathBuf {
|
||||||
let mut buf = PathBuf::new();
|
let mut buf = PathBuf::new();
|
||||||
buf.push("target");
|
buf.push("target");
|
||||||
buf.push("debug");
|
buf.push("debug");
|
||||||
buf.push("nu");
|
buf.push("nu");
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn in_directory(str: &str) -> &str {
|
pub fn in_directory(str: &str) -> &str {
|
||||||
|
Loading…
Reference in New Issue
Block a user