nushell/src/commands/classified.rs

322 lines
11 KiB
Rust
Raw Normal View History

2019-08-02 21:15:07 +02:00
use crate::commands::Command;
2019-08-09 06:51:21 +02:00
use crate::parser::{hir, TokenNode};
2019-05-22 09:12:03 +02:00
use crate::prelude::*;
2019-05-26 08:54:41 +02:00
use bytes::{BufMut, BytesMut};
2019-06-15 19:52:55 +02:00
use futures::stream::StreamExt;
2019-05-26 08:54:41 +02:00
use futures_codec::{Decoder, Encoder, Framed};
2019-06-22 05:43:37 +02:00
use log::{log_enabled, trace};
2019-05-26 08:54:41 +02:00
use std::io::{Error, ErrorKind};
2019-05-22 09:12:03 +02:00
use std::sync::Arc;
use subprocess::Exec;
2019-06-22 05:43:37 +02:00
2019-05-25 21:07:52 +02:00
/// A simple `Codec` implementation that splits up data into lines.
pub struct LinesCodec {}
impl Encoder for LinesCodec {
type Item = String;
type Error = Error;
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
dst.put(item);
Ok(())
}
}
impl Decoder for LinesCodec {
type Item = String;
type Error = Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
match src.iter().position(|b| b == &b'\n') {
Some(pos) if !src.is_empty() => {
let buf = src.split_to(pos + 1);
String::from_utf8(buf.to_vec())
.map(Some)
.map_err(|e| Error::new(ErrorKind::InvalidData, e))
}
_ if !src.is_empty() => {
let drained = src.take();
String::from_utf8(drained.to_vec())
.map(Some)
.map_err(|e| Error::new(ErrorKind::InvalidData, e))
}
2019-05-26 08:54:41 +02:00
_ => Ok(None),
2019-05-25 21:07:52 +02:00
}
}
}
2019-05-22 09:12:03 +02:00
pub(crate) struct ClassifiedInputStream {
pub(crate) objects: InputStream,
pub(crate) stdin: Option<std::fs::File>,
2019-05-24 09:29:16 +02:00
}
impl ClassifiedInputStream {
pub(crate) fn new() -> ClassifiedInputStream {
2019-05-24 09:29:16 +02:00
ClassifiedInputStream {
objects: VecDeque::new().into(),
2019-05-24 09:29:16 +02:00
stdin: None,
}
}
pub(crate) fn from_input_stream(stream: impl Into<InputStream>) -> ClassifiedInputStream {
ClassifiedInputStream {
objects: stream.into(),
2019-05-24 09:29:16 +02:00
stdin: None,
}
}
pub(crate) fn from_stdout(stdout: std::fs::File) -> ClassifiedInputStream {
2019-05-24 09:29:16 +02:00
ClassifiedInputStream {
objects: VecDeque::new().into(),
2019-05-24 09:29:16 +02:00
stdin: Some(stdout),
}
}
}
pub(crate) struct ClassifiedPipeline {
pub(crate) commands: Vec<ClassifiedCommand>,
2019-05-26 08:54:41 +02:00
}
pub(crate) enum ClassifiedCommand {
#[allow(unused)]
2019-06-22 05:43:37 +02:00
Expr(TokenNode),
2019-05-22 09:12:03 +02:00
Internal(InternalCommand),
External(ExternalCommand),
}
pub(crate) struct InternalCommand {
pub(crate) command: Arc<Command>,
pub(crate) name_tag: Tag,
pub(crate) args: hir::Call,
2019-05-22 09:12:03 +02:00
}
2019-05-24 09:29:16 +02:00
impl InternalCommand {
pub(crate) async fn run(
2019-05-24 09:29:16 +02:00
self,
context: &mut Context,
input: ClassifiedInputStream,
2019-07-24 00:22:11 +02:00
source: Text,
2019-05-24 09:29:16 +02:00
) -> Result<InputStream, ShellError> {
if log_enabled!(log::Level::Trace) {
2019-07-12 21:22:08 +02:00
trace!(target: "nu::run::internal", "->");
trace!(target: "nu::run::internal", "{}", self.command.name());
2019-07-24 00:22:11 +02:00
trace!(target: "nu::run::internal", "{}", self.args.debug(&source));
}
2019-07-12 21:22:08 +02:00
let objects: InputStream =
trace_stream!(target: "nu::trace_stream::internal", "input" = input.objects);
2019-06-22 05:43:37 +02:00
let result = context.run_command(
self.command,
self.name_tag.clone(),
context.source_map.clone(),
self.args,
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
&source,
objects,
);
2019-06-22 05:43:37 +02:00
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 result = trace_out_stream!(target: "nu::trace_stream::internal", source: &source, "output" = result);
let mut result = result.values;
2019-06-15 19:52:55 +02:00
let mut stream = VecDeque::new();
while let Some(item) = result.next().await {
match item? {
ReturnSuccess::Action(action) => match action {
2019-06-15 19:52:55 +02:00
CommandAction::ChangePath(path) => {
2019-08-07 19:49:11 +02:00
context.shell_manager.set_path(path);
2019-06-14 00:49:16 +02:00
}
CommandAction::AddSpanSource(uuid, span_source) => {
context.add_span_source(uuid, span_source);
}
2019-07-16 21:10:25 +02:00
CommandAction::Exit => std::process::exit(0),
CommandAction::EnterHelpShell(value) => {
match value {
Tagged {
item: Value::Primitive(Primitive::String(cmd)),
tag,
} => {
context.shell_manager.insert_at_current(Box::new(
HelpShell::for_command(
Value::string(cmd).tagged(tag),
&context.registry(),
)?,
));
}
_ => {
context.shell_manager.insert_at_current(Box::new(
HelpShell::index(&context.registry())?,
));
}
}
}
2019-08-14 19:02:39 +02:00
CommandAction::EnterValueShell(value) => {
2019-08-19 10:07:55 +02:00
context
.shell_manager
.insert_at_current(Box::new(ValueShell::new(value)));
2019-08-14 19:02:39 +02:00
}
2019-08-07 19:49:11 +02:00
CommandAction::EnterShell(location) => {
2019-08-31 02:59:21 +02:00
context.shell_manager.insert_at_current(Box::new(
FilesystemShell::with_location(location, context.registry().clone())?,
));
2019-08-07 19:49:11 +02:00
}
CommandAction::PreviousShell => {
context.shell_manager.prev();
}
CommandAction::NextShell => {
context.shell_manager.next();
}
CommandAction::LeaveShell => {
2019-08-19 10:07:55 +02:00
context.shell_manager.remove_at_current();
2019-08-07 19:49:11 +02:00
if context.shell_manager.is_empty() {
std::process::exit(0);
}
}
2019-06-15 19:52:55 +02:00
},
2019-05-24 09:29:16 +02:00
ReturnSuccess::Value(v) => {
2019-06-15 19:52:55 +02:00
stream.push_back(v);
}
}
}
Ok(stream.into())
2019-05-24 09:29:16 +02:00
}
}
pub(crate) struct ExternalCommand {
pub(crate) name: String,
2019-09-02 08:11:05 +02:00
pub(crate) name_tag: Tag,
pub(crate) args: Vec<Tagged<String>>,
2019-05-22 09:12:03 +02:00
}
2019-05-24 09:29:16 +02:00
pub(crate) enum StreamNext {
Last,
External,
Internal,
}
2019-05-24 09:29:16 +02:00
impl ExternalCommand {
pub(crate) async fn run(
2019-05-24 09:29:16 +02:00
self,
context: &mut Context,
2019-05-24 21:35:22 +02:00
input: ClassifiedInputStream,
stream_next: StreamNext,
2019-05-24 09:29:16 +02:00
) -> Result<ClassifiedInputStream, ShellError> {
let stdin = input.stdin;
2019-08-01 03:58:42 +02:00
let inputs: Vec<Tagged<Value>> = input.objects.into_vec().await;
let name_tag = self.name_tag.clone();
2019-07-12 21:22:08 +02:00
trace!(target: "nu::run::external", "-> {}", self.name);
trace!(target: "nu::run::external", "inputs = {:?}", inputs);
2019-06-22 05:43:37 +02:00
let mut arg_string = format!("{}", self.name);
for arg in &self.args {
2019-06-22 05:43:37 +02:00
arg_string.push_str(&arg);
}
2019-06-02 09:51:54 +02:00
let mut process;
2019-06-18 04:04:34 +02:00
2019-09-08 04:00:04 +02:00
process = Exec::cmd(&self.name);
if arg_string.contains("$it") {
let mut first = true;
2019-06-02 09:51:54 +02:00
2019-09-08 04:00:04 +02:00
for i in &inputs {
if i.as_string().is_err() {
let mut tag = None;
2019-06-02 09:51:54 +02:00
for arg in &self.args {
2019-09-08 04:00:04 +02:00
if arg.item.contains("$it") {
tag = Some(arg.tag());
2019-06-22 05:43:37 +02:00
}
2019-06-02 09:51:54 +02:00
}
if let Some(tag) = tag {
2019-09-08 04:00:04 +02:00
return Err(ShellError::labeled_error(
"External $it needs string data",
"given row instead of string data",
tag,
2019-09-08 04:00:04 +02:00
));
} else {
2019-09-08 04:00:04 +02:00
return Err(ShellError::string("Error: $it needs string data"));
}
2019-06-02 09:51:54 +02:00
}
2019-09-08 04:00:04 +02:00
if !first {
process = process.arg("&&");
process = process.arg(&self.name);
} else {
first = false;
}
2019-09-08 04:00:04 +02:00
for arg in &self.args {
if arg.chars().all(|c| c.is_whitespace()) {
continue;
2019-06-02 09:51:54 +02:00
}
2019-09-08 04:00:04 +02:00
process = process.arg(&arg.replace("$it", &i.as_string()?));
2019-06-02 09:51:54 +02:00
}
2019-09-08 04:00:04 +02:00
}
} else {
for arg in &self.args {
let arg_chars: Vec<_> = arg.chars().collect();
if arg_chars.len() > 1
&& arg_chars[0] == '"'
&& arg_chars[arg_chars.len() - 1] == '"'
{
// quoted string
let new_arg: String = arg_chars[1..arg_chars.len() - 1].iter().collect();
process = process.arg(new_arg);
} else {
process = process.arg(arg.item.clone());
2019-06-02 09:51:54 +02:00
}
}
2019-05-26 00:23:35 +02:00
}
2019-09-08 04:00:04 +02:00
2019-08-07 19:49:11 +02:00
process = process.cwd(context.shell_manager.path());
2019-05-24 09:29:16 +02:00
let mut process = match stream_next {
StreamNext::Last => process,
StreamNext::External | StreamNext::Internal => {
process.stdout(subprocess::Redirection::Pipe)
}
};
2019-05-24 09:29:16 +02:00
if let Some(stdin) = stdin {
2019-05-24 09:29:16 +02:00
process = process.stdin(stdin);
}
let mut popen = process.popen()?;
2019-05-24 09:29:16 +02:00
match stream_next {
StreamNext::Last => {
let _ = popen.detach();
loop {
match popen.poll() {
None => {
let _ = std::thread::sleep(std::time::Duration::new(0, 100000000));
}
_ => {
let _ = popen.terminate();
break;
}
}
}
println!("");
Ok(ClassifiedInputStream::new())
}
StreamNext::External => {
let stdout = popen.stdout.take().unwrap();
Ok(ClassifiedInputStream::from_stdout(stdout))
}
StreamNext::Internal => {
let stdout = popen.stdout.take().unwrap();
let file = futures::io::AllowStdIo::new(stdout);
let stream = Framed::new(file, LinesCodec {});
let stream = stream.map(move |line| Value::string(line.unwrap()).tagged(name_tag));
Ok(ClassifiedInputStream::from_input_stream(
2019-08-01 03:58:42 +02:00
stream.boxed() as BoxStream<'static, Tagged<Value>>
))
2019-05-24 09:29:16 +02:00
}
}
}
}