nushell/src/commands/open.rs

454 lines
19 KiB
Rust
Raw Normal View History

2019-08-29 05:53:45 +02:00
use crate::commands::UnevaluatedCallInfo;
use crate::context::SpanSource;
2019-05-28 06:00:00 +02:00
use crate::errors::ShellError;
2019-08-29 05:53:45 +02:00
use crate::object::Value;
2019-07-24 00:22:11 +02:00
use crate::parser::hir::SyntaxType;
2019-08-15 07:02:02 +02:00
use crate::parser::registry::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;
2019-08-24 21:36:19 +02:00
use surf::mime;
use uuid::Uuid;
2019-07-24 00:22:11 +02:00
pub struct Open;
2019-06-22 05:43:37 +02:00
2019-08-15 07:02:02 +02:00
impl PerItemCommand for Open {
2019-08-02 21:15:07 +02:00
fn name(&self) -> &str {
"open"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
2019-08-09 22:49:43 +02:00
.required("path", SyntaxType::Path)
2019-08-02 21:15:07 +02:00
.switch("raw")
}
fn usage(&self) -> &str {
"Load a file into a cell, convert to table if possible (avoid by appending '--raw')"
}
2019-07-24 00:22:11 +02:00
fn run(
&self,
2019-08-15 07:02:02 +02:00
call_info: &CallInfo,
2019-08-29 05:53:45 +02:00
registry: &CommandRegistry,
raw_args: &RawCommandArgs,
2019-08-15 07:02:02 +02:00
_input: Tagged<Value>,
2019-08-24 21:36:19 +02:00
) -> Result<OutputStream, ShellError> {
2019-08-29 05:53:45 +02:00
run(call_info, registry, raw_args)
2019-08-02 21:15:07 +02:00
}
}
2019-07-24 00:22:11 +02:00
2019-08-29 05:53:45 +02:00
fn run(
call_info: &CallInfo,
registry: &CommandRegistry,
raw_args: &RawCommandArgs,
) -> Result<OutputStream, ShellError> {
let shell_manager = &raw_args.shell_manager;
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-15 07:02:02 +02:00
let path = match call_info
.args
.nth(0)
.ok_or_else(|| ShellError::string(&format!("No file or directory specified")))?
{
file => file,
};
Add support for ~ expansion This ended up being a bit of a yak shave. The basic idea in this commit is to expand `~` in paths, but only in paths. The way this is accomplished is by doing the expansion inside of the code that parses literal syntax for `SyntaxType::Path`. As a quick refresher: every command is entitled to expand its arguments in a custom way. While this could in theory be used for general-purpose macros, today the expansion facility is limited to syntactic hints. For example, the syntax `where cpu > 0` expands under the hood to `where { $it.cpu > 0 }`. This happens because the first argument to `where` is defined as a `SyntaxType::Block`, and the parser coerces binary expressions whose left-hand-side looks like a member into a block when the command is expecting one. This is mildly more magical than what most programming languages would do, but we believe that it makes sense to allow commands to fine-tune the syntax because of the domain nushell is in (command-line shells). The syntactic expansions supported by this facility are relatively limited. For example, we don't allow `$it` to become a bare word, simply because the command asks for a string in the relevant position. That would quickly become more confusing than it's worth. This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command declares a parameter as a `SyntaxType::Path`, string literals and bare words passed as an argument to that parameter are processed using the path expansion rules. Right now, that only means that `~` is expanded into the home directory, but additional rules are possible in the future. By restricting this expansion to a syntactic expansion when passed as an argument to a command expecting a path, we avoid making `~` a generally reserved character. This will also allow us to give good tab completion for paths with `~` characters in them when a command is expecting a path. In order to accomplish the above, this commit changes the parsing functions to take a `Context` instead of just a `CommandRegistry`. From the perspective of macro expansion, you can think of the `CommandRegistry` as a dictionary of in-scope macros, and the `Context` as the compile-time state used in expansion. This could gain additional functionality over time as we find more uses for the expansion system.
2019-08-26 21:21:03 +02:00
let path_buf = path.as_path()?;
let path_str = path_buf.display().to_string();
2019-08-24 21:36:19 +02:00
let path_span = path.span();
let has_raw = call_info.args.has("raw");
2019-08-29 05:53:45 +02:00
let registry = registry.clone();
let raw_args = raw_args.clone();
2019-07-15 23:16:27 +02:00
2019-08-24 21:36:19 +02:00
let stream = async_stream_block! {
2019-08-25 15:57:47 +02:00
//FIXME: unwraps
let (file_extension, contents, contents_tag, span_source) =
fetch(&full_path, &path_str, path_span).await.unwrap();
let file_extension = if has_raw {
None
} else {
// If the extension could not be determined via mimetype, try to use the path
// extension. Some file types do not declare their mimetypes (such as bson files).
file_extension.or(path_str.split('.').last().map(String::from))
};
2019-08-24 21:36:19 +02:00
if let Some(uuid) = contents_tag.origin {
// If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource(
uuid,
span_source,
));
}
2019-08-29 05:53:45 +02:00
let tagged_contents = contents.tagged(contents_tag);
2019-08-25 15:57:47 +02:00
2019-08-29 05:53:45 +02:00
if let Some(extension) = file_extension {
let command_name = format!("from-{}", extension);
if let Some(converter) = registry.get_command(&command_name) {
let new_args = RawCommandArgs {
host: raw_args.host,
shell_manager: raw_args.shell_manager,
call_info: UnevaluatedCallInfo {
args: crate::parser::hir::Call {
head: raw_args.call_info.args.head,
positional: None,
named: None
},
source: raw_args.call_info.source,
source_map: raw_args.call_info.source_map,
name_span: raw_args.call_info.name_span,
}
2019-08-29 05:53:45 +02:00
};
let mut result = converter.run(new_args.with_input(vec![tagged_contents]), &registry);
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await;
for res in result_vec {
match res {
2019-08-31 03:35:53 +02:00
Ok(ReturnSuccess::Value(Tagged { item: Value::List(list), ..})) => {
for l in list {
yield Ok(ReturnSuccess::Value(l));
}
}
2019-08-29 05:53:45 +02:00
Ok(ReturnSuccess::Value(Tagged { item, .. })) => {
yield Ok(ReturnSuccess::Value(Tagged { item, tag: contents_tag }));
2019-08-24 21:36:19 +02:00
}
2019-08-29 05:53:45 +02:00
x => yield x,
2019-07-20 08:44:21 +02:00
}
}
2019-08-29 05:53:45 +02:00
} else {
yield ReturnSuccess::value(tagged_contents);
2019-07-24 00:22:11 +02:00
}
2019-08-29 05:53:45 +02:00
} else {
yield ReturnSuccess::value(tagged_contents);
}
2019-08-02 21:15:07 +02:00
};
2019-07-24 00:22:11 +02:00
2019-08-24 21:36:19 +02:00
Ok(stream.to_output_stream())
2019-06-22 05:43:37 +02:00
}
2019-08-24 21:36:19 +02:00
pub async fn fetch(
2019-07-02 09:56:20 +02:00
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:") {
2019-08-24 21:36:19 +02:00
let response = surf::get(location).await;
match response {
Ok(mut r) => match r.headers().get("content-type") {
Some(content_type) => {
2019-08-24 21:36:19 +02:00
let content_type = Mime::from_str(content_type).unwrap();
match (content_type.type_(), content_type.subtype()) {
(mime::APPLICATION, mime::XML) => Ok((
Some("xml".to_string()),
2019-08-24 21:36:19 +02:00
Value::string(r.body_string().await.map_err(|_| {
ShellError::labeled_error(
"Could not load text from remote url",
"could not load",
span,
)
})?),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
2019-08-24 21:36:19 +02:00
SpanSource::Url(location.to_string()),
)),
(mime::APPLICATION, mime::JSON) => Ok((
Some("json".to_string()),
2019-08-24 21:36:19 +02:00
Value::string(r.body_string().await.map_err(|_| {
ShellError::labeled_error(
"Could not load text from remote url",
"could not load",
span,
)
})?),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
2019-08-24 21:36:19 +02:00
SpanSource::Url(location.to_string()),
)),
2019-07-17 21:05:20 +02:00
(mime::APPLICATION, mime::OCTET_STREAM) => {
2019-08-24 21:36:19 +02:00
let buf: Vec<u8> = r.body_bytes().await.map_err(|_| {
2019-07-17 21:05:20 +02:00
ShellError::labeled_error(
"Could not load binary file",
"could not load",
span,
)
})?;
Ok((
None,
Value::Binary(buf),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
2019-08-24 21:36:19 +02:00
SpanSource::Url(location.to_string()),
))
2019-07-17 21:05:20 +02:00
}
2019-08-31 23:19:59 +02:00
(mime::IMAGE, mime::SVG) => Ok((
Some("svg".to_string()),
Value::string(r.body_string().await.map_err(|_| {
ShellError::labeled_error(
"Could not load svg from remote url",
"could not load",
span,
)
})?),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(location.to_string()),
)),
(mime::IMAGE, image_ty) => {
2019-08-24 21:36:19 +02:00
let buf: Vec<u8> = r.body_bytes().await.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()),
},
2019-08-24 21:36:19 +02:00
SpanSource::Url(location.to_string()),
))
}
2019-07-17 21:05:20 +02:00
(mime::TEXT, mime::HTML) => Ok((
Some("html".to_string()),
2019-08-24 21:36:19 +02:00
Value::string(r.body_string().await.map_err(|_| {
ShellError::labeled_error(
"Could not load text from remote url",
"could not load",
span,
)
})?),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
2019-08-24 21:36:19 +02:00
SpanSource::Url(location.to_string()),
2019-07-17 21:05:20 +02:00
)),
(mime::TEXT, mime::PLAIN) => {
2019-08-24 21:36:19 +02:00
let path_extension = url::Url::parse(location)
.unwrap()
.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,
2019-08-24 21:36:19 +02:00
Value::string(r.body_string().await.map_err(|_| {
ShellError::labeled_error(
"Could not load text from remote url",
"could not load",
span,
)
})?),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
2019-08-24 21:36:19 +02:00
SpanSource::Url(location.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()),
},
2019-08-24 21:36:19 +02:00
SpanSource::Url(location.to_string()),
)),
}
}
None => Ok((
None,
Value::string(format!("No content type found")),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
2019-08-24 21:36:19 +02:00
SpanSource::Url(location.to_string()),
)),
},
Err(_) => {
return Err(ShellError::labeled_error(
"URL could not be opened",
"url not found",
span,
));
}
}
} else {
cwd.push(Path::new(location));
2019-08-10 22:33:22 +02:00
if let Ok(cwd) = dunce::canonicalize(cwd) {
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()),
Value::string(s),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
)),
2019-08-12 06:11:42 +02:00
Err(_) => {
//Non utf8 data.
match (bytes.get(0), bytes.get(1)) {
(Some(x), Some(y)) if *x == 0xff && *y == 0xfe => {
// Possibly UTF-16 little endian
let utf16 = read_le_u16(&bytes[2..]);
if let Some(utf16) = utf16 {
match std::string::String::from_utf16(&utf16) {
Ok(s) => Ok((
cwd.extension()
.map(|name| name.to_string_lossy().to_string()),
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()),
)),
}
} else {
Ok((
None,
Value::Binary(bytes),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
))
}
}
(Some(x), Some(y)) if *x == 0xfe && *y == 0xff => {
// Possibly UTF-16 big endian
let utf16 = read_be_u16(&bytes[2..]);
if let Some(utf16) = utf16 {
match std::string::String::from_utf16(&utf16) {
Ok(s) => Ok((
cwd.extension()
.map(|name| name.to_string_lossy().to_string()),
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()),
)),
}
} else {
Ok((
None,
Value::Binary(bytes),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
))
}
}
_ => Ok((
None,
Value::Binary(bytes),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
)),
}
}
2019-08-10 22:33:22 +02:00
},
Err(_) => {
return Err(ShellError::labeled_error(
"File could not be opened",
"file not found",
span,
2019-08-10 22:33:22 +02:00
));
}
}
2019-08-10 22:33:22 +02:00
} else {
return Err(ShellError::labeled_error(
"File could not be opened",
"file not found",
span,
));
}
}
}
2019-08-12 06:11:42 +02:00
fn read_le_u16(input: &[u8]) -> Option<Vec<u16>> {
if input.len() % 2 != 0 || input.len() < 2 {
None
} else {
let mut result = vec![];
let mut pos = 0;
while pos < input.len() {
result.push(u16::from_le_bytes([input[pos], input[pos + 1]]));
pos += 2;
}
Some(result)
}
}
fn read_be_u16(input: &[u8]) -> Option<Vec<u16>> {
if input.len() % 2 != 0 || input.len() < 2 {
None
} else {
let mut result = vec![];
let mut pos = 0;
while pos < input.len() {
result.push(u16::from_be_bytes([input[pos], input[pos + 1]]));
pos += 2;
}
Some(result)
}
}