nushell/src/commands/open.rs

393 lines
14 KiB
Rust
Raw Normal View History

2019-08-02 21:15:07 +02:00
use crate::commands::StaticCommand;
use crate::context::SpanSource;
2019-05-28 06:00:00 +02:00
use crate::errors::ShellError;
2019-07-24 06:10:48 +02:00
use crate::object::{Primitive, Value};
2019-07-24 00:22:11 +02:00
use crate::parser::hir::SyntaxType;
2019-08-02 21:15:07 +02:00
use crate::parser::registry::{self, Signature};
2019-05-28 06:00:00 +02:00
use crate::prelude::*;
use mime::Mime;
2019-06-03 09:41:28 +02:00
use std::path::{Path, PathBuf};
use std::str::FromStr;
use uuid::Uuid;
2019-05-28 06:00:00 +02:00
2019-07-24 00:22:11 +02:00
pub struct Open;
2019-06-22 05:43:37 +02:00
2019-08-02 21:15:07 +02:00
#[derive(Deserialize)]
pub struct OpenArgs {
2019-08-09 06:51:21 +02:00
path: Tagged<PathBuf>,
2019-08-02 21:15:07 +02:00
raw: bool,
}
impl StaticCommand for Open {
fn name(&self) -> &str {
"open"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required("path", SyntaxType::Block)
.switch("raw")
}
2019-07-24 00:22:11 +02:00
fn run(
&self,
args: CommandArgs,
registry: &registry::CommandRegistry,
) -> Result<OutputStream, ShellError> {
2019-08-02 21:15:07 +02:00
args.process(registry, run)?.run()
}
}
2019-07-24 00:22:11 +02:00
2019-08-02 21:15:07 +02:00
fn run(
OpenArgs { raw, path }: OpenArgs,
2019-08-09 06:51:21 +02:00
RunnableContext {
shell_manager,
name,
..
}: RunnableContext,
2019-08-02 21:15:07 +02:00
) -> Result<OutputStream, ShellError> {
2019-08-09 06:51:21 +02:00
let cwd = PathBuf::from(shell_manager.path());
2019-08-02 21:15:07 +02:00
let full_path = PathBuf::from(cwd);
2019-06-22 05:43:37 +02:00
2019-08-02 21:15:07 +02:00
let path_str = path.to_str().ok_or(ShellError::type_error(
"Path",
2019-08-09 06:51:21 +02:00
"invalid path".tagged(path.tag()),
2019-08-02 21:15:07 +02:00
))?;
2019-07-15 23:16:27 +02:00
2019-08-09 06:51:21 +02:00
let (file_extension, contents, contents_tag, span_source) =
fetch(&full_path, path_str, path.span())?;
2019-08-02 21:15:07 +02:00
let file_extension = if raw { None } else { file_extension };
2019-08-02 21:15:07 +02:00
let mut stream = VecDeque::new();
2019-08-09 06:51:21 +02:00
if let Some(uuid) = contents_tag.origin {
2019-08-02 21:15:07 +02:00
// If we have loaded something, track its source
stream.push_back(ReturnSuccess::action(CommandAction::AddSpanSource(
uuid,
span_source,
)))
}
2019-08-02 21:15:07 +02:00
match contents {
Value::Primitive(Primitive::String(string)) => {
2019-08-09 06:51:21 +02:00
let value = parse_as_value(file_extension, string, contents_tag, name)?;
2019-08-02 21:15:07 +02:00
match value {
2019-08-09 06:51:21 +02:00
Tagged {
2019-08-02 21:15:07 +02:00
item: Value::List(list),
..
} => {
for elem in list {
stream.push_back(ReturnSuccess::value(elem));
2019-07-20 08:44:21 +02:00
}
}
2019-08-02 21:15:07 +02:00
x => stream.push_back(ReturnSuccess::value(x)),
2019-07-24 00:22:11 +02:00
}
2019-08-02 21:15:07 +02:00
}
2019-07-13 04:07:06 +02:00
2019-08-09 06:51:21 +02:00
other => stream.push_back(ReturnSuccess::value(other.tagged(contents_tag))),
2019-08-02 21:15:07 +02:00
};
2019-07-24 00:22:11 +02:00
2019-08-02 21:15:07 +02:00
Ok(stream.boxed().to_output_stream())
2019-06-22 05:43:37 +02:00
}
2019-07-24 00:22:11 +02:00
// command! {
// Open as open(args, path: Spanned<PathBuf>, --raw: Switch,) {
// let span = args.name_span();
// let env = args.env.clone();
// let path = env
// .lock()
// .unwrap()
// .path()
// .to_path_buf();
// let full_path = PathBuf::from(cwd);
// let path_str = path.to_str().ok_or(ShellError::type_error("Path", "invalid path".spanned(path.span)))?;
// let (file_extension, contents, contents_span, span_source) = fetch(&full_path, path_str, path.span)?;
// let file_extension = if raw.is_present() {
// None
// } else {
// file_extension
// };
// let mut stream = VecDeque::new();
// if let Some(uuid) = contents_span.source {
// // If we have loaded something, track its source
// stream.push_back(ReturnSuccess::action(CommandAction::AddSpanSource(uuid, span_source)))
// }
// match contents {
// Value::Primitive(Primitive::String(string)) => {
// let value = parse_as_value(
// file_extension,
// string,
// contents_span,
// span,
// )?;
// match value {
// Spanned { item: Value::List(list), .. } => {
// for elem in list {
// stream.push_back(ReturnSuccess::value(elem));
// }
// }
// x => stream.push_back(ReturnSuccess::value(x))
// }
// },
// other => stream.push_back(ReturnSuccess::value(other.spanned(contents_span))),
// };
// stream
// }
// }
2019-07-02 09:56:20 +02:00
pub fn fetch(
cwd: &PathBuf,
location: &str,
span: Span,
) -> Result<(Option<String>, Value, Tag, SpanSource), ShellError> {
let mut cwd = cwd.clone();
if location.starts_with("http:") || location.starts_with("https:") {
let response = reqwest::get(location);
match response {
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()),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(r.url().to_string()),
)),
(mime::APPLICATION, mime::JSON) => Ok((
Some("json".to_string()),
Value::string(r.text().unwrap()),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(r.url().to_string()),
)),
2019-07-17 21:05:20 +02:00
(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),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(r.url().to_string()),
))
2019-07-17 21:05:20 +02:00
}
(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),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(r.url().to_string()),
))
}
2019-07-17 21:05:20 +02:00
(mime::TEXT, mime::HTML) => Ok((
Some("html".to_string()),
Value::string(r.text().unwrap()),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(r.url().to_string()),
2019-07-17 21:05:20 +02:00
)),
(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((
path_extension,
Value::string(r.text().unwrap()),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(r.url().to_string()),
))
}
(ty, sub_ty) => Ok((
None,
2019-07-30 05:48:02 +02:00
Value::string(format!(
"Not yet supported MIME type: {} {}",
ty, sub_ty
)),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(r.url().to_string()),
)),
}
}
None => Ok((
None,
Value::string(format!("No content type found")),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(r.url().to_string()),
)),
},
Err(_) => {
return Err(ShellError::labeled_error(
"URL could not be opened",
"url not found",
span,
));
}
}
} else {
cwd.push(Path::new(location));
2019-07-04 07:11:56 +02:00
match std::fs::read(&cwd) {
Ok(bytes) => match std::str::from_utf8(&bytes) {
Ok(s) => Ok((
cwd.extension()
.map(|name| name.to_string_lossy().to_string()),
2019-07-13 04:07:06 +02:00
Value::string(s),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
)),
Err(_) => Ok((
None,
Value::Binary(bytes),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
2019-07-04 07:11:56 +02:00
)),
},
Err(_) => {
return Err(ShellError::labeled_error(
2019-07-21 09:08:05 +02:00
"File could not be opened",
"file not found",
span,
));
}
}
}
}
pub fn parse_as_value(
extension: Option<String>,
contents: String,
contents_tag: Tag,
name_span: Span,
2019-08-01 03:58:42 +02:00
) -> Result<Tagged<Value>, ShellError> {
match extension {
Some(x) if x == "csv" => crate::commands::from_csv::from_csv_string_to_value(
contents,
contents_tag,
)
.map_err(move |_| {
ShellError::labeled_error("Could not open as CSV", "could not open as CSV", name_span)
}),
2019-07-09 06:31:26 +02:00
Some(x) if x == "toml" => {
crate::commands::from_toml::from_toml_string_to_value(contents, contents_tag).map_err(
move |_| {
ShellError::labeled_error(
2019-07-09 06:31:26 +02:00
"Could not open as TOML",
"could not open as TOML",
name_span,
)
},
)
2019-07-09 06:31:26 +02:00
}
Some(x) if x == "json" => {
crate::commands::from_json::from_json_string_to_value(contents, contents_tag).map_err(
move |_| {
ShellError::labeled_error(
2019-07-09 06:31:26 +02:00
"Could not open as JSON",
"could not open as JSON",
name_span,
)
},
)
}
Some(x) if x == "ini" => crate::commands::from_ini::from_ini_string_to_value(
contents,
contents_tag,
)
.map_err(move |_| {
ShellError::labeled_error("Could not open as INI", "could not open as INI", name_span)
}),
Some(x) if x == "xml" => crate::commands::from_xml::from_xml_string_to_value(
contents,
contents_tag,
)
.map_err(move |_| {
ShellError::labeled_error("Could not open as XML", "could not open as XML", name_span)
}),
2019-07-09 06:31:26 +02:00
Some(x) if x == "yml" => {
crate::commands::from_yaml::from_yaml_string_to_value(contents, contents_tag).map_err(
2019-07-09 06:31:26 +02:00
move |_| {
ShellError::labeled_error(
2019-07-09 06:31:26 +02:00
"Could not open as YAML",
"could not open as YAML",
name_span,
)
},
)
}
Some(x) if x == "yaml" => {
crate::commands::from_yaml::from_yaml_string_to_value(contents, contents_tag).map_err(
2019-07-09 06:31:26 +02:00
move |_| {
ShellError::labeled_error(
2019-07-09 06:31:26 +02:00
"Could not open as YAML",
"could not open as YAML",
name_span,
)
},
)
}
_ => Ok(Value::string(contents).tagged(contents_tag)),
}
}