2019-09-03 06:04:46 +00:00
|
|
|
use crate::commands::UnevaluatedCallInfo;
|
2019-09-29 05:18:59 +00:00
|
|
|
use crate::context::AnchorLocation;
|
2019-09-18 06:37:04 +00:00
|
|
|
use crate::data::meta::Span;
|
2019-09-05 16:23:42 +00:00
|
|
|
use crate::data::Value;
|
2019-09-03 06:04:46 +00:00
|
|
|
use crate::errors::ShellError;
|
2019-09-14 16:30:24 +00:00
|
|
|
use crate::parser::hir::SyntaxShape;
|
2019-09-03 06:04:46 +00:00
|
|
|
use crate::parser::registry::Signature;
|
|
|
|
use crate::prelude::*;
|
|
|
|
use mime::Mime;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::str::FromStr;
|
|
|
|
use surf::mime;
|
2019-09-18 06:37:04 +00:00
|
|
|
use uuid::Uuid;
|
2019-09-03 06:04:46 +00:00
|
|
|
pub struct Fetch;
|
|
|
|
|
|
|
|
impl PerItemCommand for Fetch {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"fetch"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build(self.name())
|
2019-09-14 16:30:24 +00:00
|
|
|
.required("path", SyntaxShape::Path)
|
2019-09-03 06:04:46 +00:00
|
|
|
.switch("raw")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Load from a URL into a cell, convert to table if possible (avoid by appending '--raw')"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
call_info: &CallInfo,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
raw_args: &RawCommandArgs,
|
|
|
|
_input: Tagged<Value>,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
run(call_info, registry, raw_args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
call_info: &CallInfo,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
raw_args: &RawCommandArgs,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let path = match call_info
|
|
|
|
.args
|
|
|
|
.nth(0)
|
|
|
|
.ok_or_else(|| ShellError::string(&format!("No file or directory specified")))?
|
|
|
|
{
|
|
|
|
file => file,
|
|
|
|
};
|
|
|
|
let path_buf = path.as_path()?;
|
|
|
|
let path_str = path_buf.display().to_string();
|
Overhaul the expansion system
The main thrust of this (very large) commit is an overhaul of the
expansion system.
The parsing pipeline is:
- Lightly parse the source file for atoms, basic delimiters and pipeline
structure into a token tree
- Expand the token tree into a HIR (high-level intermediate
representation) based upon the baseline syntax rules for expressions
and the syntactic shape of commands.
Somewhat non-traditionally, nu doesn't have an AST at all. It goes
directly from the token tree, which doesn't represent many important
distinctions (like the difference between `hello` and `5KB`) directly
into a high-level representation that doesn't have a direct
correspondence to the source code.
At a high level, nu commands work like macros, in the sense that the
syntactic shape of the invocation of a command depends on the
definition of a command.
However, commands do not have the ability to perform unrestricted
expansions of the token tree. Instead, they describe their arguments in
terms of syntactic shapes, and the expander expands the token tree into
HIR based upon that definition.
For example, the `where` command says that it takes a block as its first
required argument, and the description of the block syntactic shape
expands the syntax `cpu > 10` into HIR that represents
`{ $it.cpu > 10 }`.
This commit overhauls that system so that the syntactic shapes are
described in terms of a few new traits (`ExpandSyntax` and
`ExpandExpression` are the primary ones) that are more composable than
the previous system.
The first big win of this new system is the addition of the `ColumnPath`
shape, which looks like `cpu."max ghz"` or `package.version`.
Previously, while a variable path could look like `$it.cpu."max ghz"`,
the tail of a variable path could not be easily reused in other
contexts. Now, that tail is its own syntactic shape, and it can be used
as part of a command's signature.
This cleans up commands like `inc`, `add` and `edit` as well as
shorthand blocks, which can now look like `| where cpu."max ghz" > 10`
2019-09-17 22:26:27 +00:00
|
|
|
let path_span = path.tag.span;
|
2019-09-03 06:04:46 +00:00
|
|
|
let has_raw = call_info.args.has("raw");
|
|
|
|
let registry = registry.clone();
|
|
|
|
let raw_args = raw_args.clone();
|
|
|
|
|
2019-09-26 00:22:17 +00:00
|
|
|
let stream = async_stream! {
|
2019-09-03 06:04:46 +00:00
|
|
|
|
2019-09-18 06:37:04 +00:00
|
|
|
let result = fetch(&path_str, path_span).await;
|
2019-09-03 06:04:46 +00:00
|
|
|
|
|
|
|
if let Err(e) = result {
|
|
|
|
yield Err(e);
|
|
|
|
return;
|
|
|
|
}
|
2019-09-29 05:18:59 +00:00
|
|
|
let (file_extension, contents, contents_tag, anchor_location) = result.unwrap();
|
2019-09-03 06:04:46 +00:00
|
|
|
|
|
|
|
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-09-29 05:13:56 +00:00
|
|
|
if contents_tag.anchor != uuid::Uuid::nil() {
|
2019-09-03 06:04:46 +00:00
|
|
|
// If we have loaded something, track its source
|
2019-09-29 05:18:59 +00:00
|
|
|
yield ReturnSuccess::action(CommandAction::AddAnchorLocation(
|
2019-09-29 05:13:56 +00:00
|
|
|
contents_tag.anchor,
|
2019-09-29 05:18:59 +00:00
|
|
|
anchor_location,
|
2019-09-03 06:04:46 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let tagged_contents = contents.tagged(contents_tag);
|
|
|
|
|
|
|
|
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,
|
2019-09-14 16:30:24 +00:00
|
|
|
name_tag: raw_args.call_info.name_tag,
|
2019-09-03 06:04:46 +00:00
|
|
|
}
|
|
|
|
};
|
2019-09-17 02:09:15 +00:00
|
|
|
let mut result = converter.run(new_args.with_input(vec![tagged_contents]), ®istry, false);
|
2019-09-03 06:04:46 +00:00
|
|
|
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await;
|
|
|
|
for res in result_vec {
|
|
|
|
match res {
|
2019-09-05 16:23:42 +00:00
|
|
|
Ok(ReturnSuccess::Value(Tagged { item: Value::Table(list), ..})) => {
|
2019-09-03 06:04:46 +00:00
|
|
|
for l in list {
|
|
|
|
yield Ok(ReturnSuccess::Value(l));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(ReturnSuccess::Value(Tagged { item, .. })) => {
|
|
|
|
yield Ok(ReturnSuccess::Value(Tagged { item, tag: contents_tag }));
|
|
|
|
}
|
|
|
|
x => yield x,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
yield ReturnSuccess::value(tagged_contents);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
yield ReturnSuccess::value(tagged_contents);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn fetch(
|
|
|
|
location: &str,
|
2019-09-18 06:37:04 +00:00
|
|
|
span: Span,
|
2019-09-29 05:18:59 +00:00
|
|
|
) -> Result<(Option<String>, Value, Tag, AnchorLocation), ShellError> {
|
2019-09-03 06:04:46 +00:00
|
|
|
if let Err(_) = url::Url::parse(location) {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Incomplete or incorrect url",
|
|
|
|
"expected a full url",
|
2019-09-18 06:37:04 +00:00
|
|
|
span,
|
2019-09-03 06:04:46 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let response = surf::get(location).await;
|
|
|
|
match response {
|
|
|
|
Ok(mut r) => match r.headers().get("content-type") {
|
|
|
|
Some(content_type) => {
|
|
|
|
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()),
|
|
|
|
Value::string(r.body_string().await.map_err(|_| {
|
|
|
|
ShellError::labeled_error(
|
|
|
|
"Could not load text from remote url",
|
|
|
|
"could not load",
|
2019-09-18 06:37:04 +00:00
|
|
|
span,
|
2019-09-03 06:04:46 +00:00
|
|
|
)
|
|
|
|
})?),
|
2019-09-18 06:37:04 +00:00
|
|
|
Tag {
|
|
|
|
span,
|
2019-09-29 05:13:56 +00:00
|
|
|
anchor: Uuid::new_v4(),
|
2019-09-18 06:37:04 +00:00
|
|
|
},
|
2019-09-29 05:18:59 +00:00
|
|
|
AnchorLocation::Url(location.to_string()),
|
2019-09-03 06:04:46 +00:00
|
|
|
)),
|
|
|
|
(mime::APPLICATION, mime::JSON) => Ok((
|
|
|
|
Some("json".to_string()),
|
|
|
|
Value::string(r.body_string().await.map_err(|_| {
|
|
|
|
ShellError::labeled_error(
|
|
|
|
"Could not load text from remote url",
|
|
|
|
"could not load",
|
2019-09-18 06:37:04 +00:00
|
|
|
span,
|
2019-09-03 06:04:46 +00:00
|
|
|
)
|
|
|
|
})?),
|
2019-09-18 06:37:04 +00:00
|
|
|
Tag {
|
|
|
|
span,
|
2019-09-29 05:13:56 +00:00
|
|
|
anchor: Uuid::new_v4(),
|
2019-09-18 06:37:04 +00:00
|
|
|
},
|
2019-09-29 05:18:59 +00:00
|
|
|
AnchorLocation::Url(location.to_string()),
|
2019-09-03 06:04:46 +00:00
|
|
|
)),
|
|
|
|
(mime::APPLICATION, mime::OCTET_STREAM) => {
|
|
|
|
let buf: Vec<u8> = r.body_bytes().await.map_err(|_| {
|
|
|
|
ShellError::labeled_error(
|
|
|
|
"Could not load binary file",
|
|
|
|
"could not load",
|
2019-09-18 06:37:04 +00:00
|
|
|
span,
|
2019-09-03 06:04:46 +00:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
Ok((
|
|
|
|
None,
|
2019-09-14 16:30:24 +00:00
|
|
|
Value::binary(buf),
|
2019-09-18 06:37:04 +00:00
|
|
|
Tag {
|
|
|
|
span,
|
2019-09-29 05:13:56 +00:00
|
|
|
anchor: Uuid::new_v4(),
|
2019-09-18 06:37:04 +00:00
|
|
|
},
|
2019-09-29 05:18:59 +00:00
|
|
|
AnchorLocation::Url(location.to_string()),
|
2019-09-03 06:04:46 +00: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",
|
2019-09-18 06:37:04 +00:00
|
|
|
span,
|
2019-09-03 06:04:46 +00:00
|
|
|
)
|
|
|
|
})?),
|
2019-09-18 06:37:04 +00:00
|
|
|
Tag {
|
|
|
|
span,
|
2019-09-29 05:13:56 +00:00
|
|
|
anchor: Uuid::new_v4(),
|
2019-09-18 06:37:04 +00:00
|
|
|
},
|
2019-09-29 05:18:59 +00:00
|
|
|
AnchorLocation::Url(location.to_string()),
|
2019-09-03 06:04:46 +00:00
|
|
|
)),
|
|
|
|
(mime::IMAGE, image_ty) => {
|
|
|
|
let buf: Vec<u8> = r.body_bytes().await.map_err(|_| {
|
|
|
|
ShellError::labeled_error(
|
|
|
|
"Could not load image file",
|
|
|
|
"could not load",
|
2019-09-18 06:37:04 +00:00
|
|
|
span,
|
2019-09-03 06:04:46 +00:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
Ok((
|
|
|
|
Some(image_ty.to_string()),
|
2019-09-14 16:30:24 +00:00
|
|
|
Value::binary(buf),
|
2019-09-18 06:37:04 +00:00
|
|
|
Tag {
|
|
|
|
span,
|
2019-09-29 05:13:56 +00:00
|
|
|
anchor: Uuid::new_v4(),
|
2019-09-18 06:37:04 +00:00
|
|
|
},
|
2019-09-29 05:18:59 +00:00
|
|
|
AnchorLocation::Url(location.to_string()),
|
2019-09-03 06:04:46 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
(mime::TEXT, mime::HTML) => Ok((
|
|
|
|
Some("html".to_string()),
|
|
|
|
Value::string(r.body_string().await.map_err(|_| {
|
|
|
|
ShellError::labeled_error(
|
|
|
|
"Could not load text from remote url",
|
|
|
|
"could not load",
|
2019-09-18 06:37:04 +00:00
|
|
|
span,
|
2019-09-03 06:04:46 +00:00
|
|
|
)
|
|
|
|
})?),
|
2019-09-18 06:37:04 +00:00
|
|
|
Tag {
|
|
|
|
span,
|
2019-09-29 05:13:56 +00:00
|
|
|
anchor: Uuid::new_v4(),
|
2019-09-18 06:37:04 +00:00
|
|
|
},
|
2019-09-29 05:18:59 +00:00
|
|
|
AnchorLocation::Url(location.to_string()),
|
2019-09-03 06:04:46 +00:00
|
|
|
)),
|
|
|
|
(mime::TEXT, mime::PLAIN) => {
|
|
|
|
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,
|
|
|
|
Value::string(r.body_string().await.map_err(|_| {
|
|
|
|
ShellError::labeled_error(
|
|
|
|
"Could not load text from remote url",
|
|
|
|
"could not load",
|
2019-09-18 06:37:04 +00:00
|
|
|
span,
|
2019-09-03 06:04:46 +00:00
|
|
|
)
|
|
|
|
})?),
|
2019-09-18 06:37:04 +00:00
|
|
|
Tag {
|
|
|
|
span,
|
2019-09-29 05:13:56 +00:00
|
|
|
anchor: Uuid::new_v4(),
|
2019-09-18 06:37:04 +00:00
|
|
|
},
|
2019-09-29 05:18:59 +00:00
|
|
|
AnchorLocation::Url(location.to_string()),
|
2019-09-03 06:04:46 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
(ty, sub_ty) => Ok((
|
|
|
|
None,
|
|
|
|
Value::string(format!("Not yet supported MIME type: {} {}", ty, sub_ty)),
|
2019-09-18 06:37:04 +00:00
|
|
|
Tag {
|
|
|
|
span,
|
2019-09-29 05:13:56 +00:00
|
|
|
anchor: Uuid::new_v4(),
|
2019-09-18 06:37:04 +00:00
|
|
|
},
|
2019-09-29 05:18:59 +00:00
|
|
|
AnchorLocation::Url(location.to_string()),
|
2019-09-03 06:04:46 +00:00
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Ok((
|
|
|
|
None,
|
|
|
|
Value::string(format!("No content type found")),
|
2019-09-18 06:37:04 +00:00
|
|
|
Tag {
|
|
|
|
span,
|
2019-09-29 05:13:56 +00:00
|
|
|
anchor: Uuid::new_v4(),
|
2019-09-18 06:37:04 +00:00
|
|
|
},
|
2019-09-29 05:18:59 +00:00
|
|
|
AnchorLocation::Url(location.to_string()),
|
2019-09-03 06:04:46 +00:00
|
|
|
)),
|
|
|
|
},
|
|
|
|
Err(_) => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"URL could not be opened",
|
|
|
|
"url not found",
|
2019-09-18 06:37:04 +00:00
|
|
|
span,
|
2019-09-03 06:04:46 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|