2021-10-25 08:31:39 +02:00
|
|
|
use std::collections::HashMap;
|
2021-09-29 20:25:05 +02:00
|
|
|
use std::io::{BufRead, BufReader, Write};
|
2022-01-24 19:26:56 +01:00
|
|
|
use std::path::PathBuf;
|
2021-10-26 00:56:29 +02:00
|
|
|
use std::process::{Command as CommandSys, Stdio};
|
2021-11-09 07:14:00 +01:00
|
|
|
use std::sync::atomic::Ordering;
|
2021-09-29 20:25:05 +02:00
|
|
|
use std::sync::mpsc;
|
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
use nu_engine::env_to_strings;
|
2021-10-25 08:31:39 +02:00
|
|
|
use nu_protocol::engine::{EngineState, Stack};
|
2021-10-25 18:58:58 +02:00
|
|
|
use nu_protocol::{ast::Call, engine::Command, ShellError, Signature, SyntaxShape, Value};
|
2022-02-25 20:51:31 +01:00
|
|
|
use nu_protocol::{Category, Example, ListStream, PipelineData, RawStream, Span, Spanned};
|
2021-10-25 18:58:58 +02:00
|
|
|
|
2021-12-02 21:55:16 +01:00
|
|
|
use itertools::Itertools;
|
|
|
|
|
2021-10-25 18:58:58 +02:00
|
|
|
use nu_engine::CallExt;
|
2022-01-24 19:26:56 +01:00
|
|
|
use pathdiff::diff_paths;
|
2021-12-02 21:55:16 +01:00
|
|
|
use regex::Regex;
|
2021-09-29 20:25:05 +02:00
|
|
|
|
2021-12-24 08:22:11 +01:00
|
|
|
const OUTPUT_BUFFER_SIZE: usize = 1024;
|
2022-03-27 04:34:34 +02:00
|
|
|
const OUTPUT_BUFFERS_IN_FLIGHT: usize = 3;
|
2021-09-29 20:25:05 +02:00
|
|
|
|
2021-10-25 06:01:02 +02:00
|
|
|
#[derive(Clone)]
|
2021-09-29 20:25:05 +02:00
|
|
|
pub struct External;
|
|
|
|
|
|
|
|
impl Command for External {
|
|
|
|
fn name(&self) -> &str {
|
2022-02-17 15:55:17 +01:00
|
|
|
"run-external"
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Runs external command"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
2022-02-22 14:13:38 +01:00
|
|
|
Signature::build(self.name())
|
2022-02-21 23:22:21 +01:00
|
|
|
.switch("redirect-stdout", "redirect-stdout", None)
|
|
|
|
.switch("redirect-stderr", "redirect-stderr", None)
|
2021-09-29 20:25:05 +02:00
|
|
|
.rest("rest", SyntaxShape::Any, "external command to run")
|
2021-11-17 05:22:37 +01:00
|
|
|
.category(Category::System)
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2021-10-25 08:31:39 +02:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-09-29 20:25:05 +02:00
|
|
|
call: &Call,
|
2021-10-25 06:01:02 +02:00
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
2021-12-27 04:04:22 +01:00
|
|
|
let name: Spanned<String> = call.req(engine_state, stack, 0)?;
|
2021-12-22 09:13:05 +01:00
|
|
|
let args: Vec<Value> = call.rest(engine_state, stack, 1)?;
|
2022-02-21 23:22:21 +01:00
|
|
|
let redirect_stdout = call.has_flag("redirect-stdout");
|
|
|
|
let redirect_stderr = call.has_flag("redirect-stderr");
|
2021-10-25 08:31:39 +02:00
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
// Translate environment variables from Values to Strings
|
2022-03-11 23:18:39 +01:00
|
|
|
let env_vars_str = env_to_strings(engine_state, stack)?;
|
2021-11-14 20:25:57 +01:00
|
|
|
|
2022-02-24 06:09:02 +01:00
|
|
|
fn value_as_spanned(value: Value) -> Result<Spanned<String>, ShellError> {
|
|
|
|
let span = value.span()?;
|
|
|
|
|
|
|
|
value
|
|
|
|
.as_string()
|
|
|
|
.map(|item| Spanned { item, span })
|
|
|
|
.map_err(|_| {
|
|
|
|
ShellError::ExternalCommand(
|
|
|
|
"Cannot convert argument to a string".into(),
|
|
|
|
"All arguments to an external command need to be string-compatible".into(),
|
|
|
|
span,
|
|
|
|
)
|
|
|
|
})
|
2021-12-22 09:13:05 +01:00
|
|
|
}
|
|
|
|
|
2022-02-24 06:09:02 +01:00
|
|
|
let args = args
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|arg| match arg {
|
|
|
|
Value::List { vals, .. } => vals
|
|
|
|
.into_iter()
|
|
|
|
.map(value_as_spanned)
|
|
|
|
.collect::<Vec<Result<Spanned<String>, ShellError>>>(),
|
|
|
|
val => vec![value_as_spanned(val)],
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<Spanned<String>>, ShellError>>()?;
|
|
|
|
|
2021-10-25 08:31:39 +02:00
|
|
|
let command = ExternalCommand {
|
|
|
|
name,
|
2022-02-24 06:09:02 +01:00
|
|
|
args,
|
2022-02-21 23:22:21 +01:00
|
|
|
redirect_stdout,
|
|
|
|
redirect_stderr,
|
2021-12-17 02:04:54 +01:00
|
|
|
env_vars: env_vars_str,
|
2021-10-25 08:31:39 +02:00
|
|
|
};
|
2022-01-31 13:52:05 +01:00
|
|
|
command.run_with_input(engine_state, stack, input)
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
2022-02-25 11:19:25 +01:00
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
|
|
|
description: "Run an external command",
|
|
|
|
example: r#"run-external "echo" "-n" "hello""#,
|
|
|
|
result: None,
|
|
|
|
}]
|
|
|
|
}
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 18:27:35 +01:00
|
|
|
pub struct ExternalCommand {
|
2021-10-25 08:31:39 +02:00
|
|
|
pub name: Spanned<String>,
|
2022-01-24 19:26:56 +01:00
|
|
|
pub args: Vec<Spanned<String>>,
|
2022-02-21 23:22:21 +01:00
|
|
|
pub redirect_stdout: bool,
|
|
|
|
pub redirect_stderr: bool,
|
2021-10-25 08:31:39 +02:00
|
|
|
pub env_vars: HashMap<String, String>,
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 18:27:35 +01:00
|
|
|
impl ExternalCommand {
|
2021-10-28 06:13:10 +02:00
|
|
|
pub fn run_with_input(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
2022-01-31 13:52:05 +01:00
|
|
|
stack: &mut Stack,
|
2021-10-28 06:13:10 +02:00
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
2021-12-24 08:22:11 +01:00
|
|
|
let head = self.name.span;
|
2021-09-29 20:25:05 +02:00
|
|
|
|
2021-10-28 06:13:10 +02:00
|
|
|
let ctrlc = engine_state.ctrlc.clone();
|
|
|
|
|
2022-02-22 14:13:38 +01:00
|
|
|
let mut process = self.create_process(&input, false, head)?;
|
2022-01-31 18:42:12 +01:00
|
|
|
let child;
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
match process.spawn() {
|
|
|
|
Err(_) => {
|
2022-02-22 14:13:38 +01:00
|
|
|
let mut process = self.create_process(&input, true, head)?;
|
2022-01-31 18:42:12 +01:00
|
|
|
child = process.spawn();
|
|
|
|
}
|
|
|
|
Ok(process) => {
|
|
|
|
child = Ok(process);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
{
|
|
|
|
child = process.spawn()
|
|
|
|
}
|
|
|
|
|
|
|
|
match child {
|
2021-09-29 20:25:05 +02:00
|
|
|
Err(err) => Err(ShellError::ExternalCommand(
|
2022-01-22 15:12:34 +01:00
|
|
|
"can't run executable".to_string(),
|
|
|
|
err.to_string(),
|
2021-09-29 20:25:05 +02:00
|
|
|
self.name.span,
|
|
|
|
)),
|
|
|
|
Ok(mut child) => {
|
2022-01-31 13:52:05 +01:00
|
|
|
if !input.is_nothing() {
|
|
|
|
let engine_state = engine_state.clone();
|
|
|
|
let mut stack = stack.clone();
|
|
|
|
stack.update_config(
|
|
|
|
"use_ansi_coloring",
|
|
|
|
Value::Bool {
|
|
|
|
val: false,
|
|
|
|
span: Span::new(0, 0),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
// if there is a string or a stream, that is sent to the pipe std
|
|
|
|
if let Some(mut stdin_write) = child.stdin.take() {
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
let input = crate::Table::run(
|
|
|
|
&crate::Table,
|
|
|
|
&engine_state,
|
|
|
|
&mut stack,
|
2022-02-04 03:01:45 +01:00
|
|
|
&Call::new(head),
|
2022-01-31 13:52:05 +01:00
|
|
|
input,
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Ok(input) = input {
|
|
|
|
for value in input.into_iter() {
|
2022-03-27 04:35:59 +02:00
|
|
|
let buf = match value {
|
|
|
|
Value::String { val, .. } => val.into_bytes(),
|
|
|
|
Value::Binary { val, .. } => val,
|
|
|
|
_ => return Err(()),
|
|
|
|
};
|
|
|
|
if stdin_write.write(&buf).is_err() {
|
|
|
|
return Ok(());
|
2021-10-26 00:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
2022-01-31 13:52:05 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
2022-02-21 23:22:21 +01:00
|
|
|
let redirect_stdout = self.redirect_stdout;
|
|
|
|
let redirect_stderr = self.redirect_stderr;
|
2021-11-09 07:14:00 +01:00
|
|
|
let span = self.name.span;
|
|
|
|
let output_ctrlc = ctrlc.clone();
|
2022-03-27 04:34:34 +02:00
|
|
|
let (stdout_tx, stdout_rx) = mpsc::sync_channel(OUTPUT_BUFFERS_IN_FLIGHT);
|
|
|
|
let (stderr_tx, stderr_rx) = mpsc::sync_channel(OUTPUT_BUFFERS_IN_FLIGHT);
|
2022-02-25 20:51:31 +01:00
|
|
|
let (exit_code_tx, exit_code_rx) = mpsc::channel();
|
2021-11-09 07:14:00 +01:00
|
|
|
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
// If this external is not the last expression, then its output is piped to a channel
|
2022-02-24 12:57:31 +01:00
|
|
|
// and we create a ListStream that can be consumed
|
2022-02-21 23:22:21 +01:00
|
|
|
|
|
|
|
if redirect_stderr {
|
2022-02-25 20:51:31 +01:00
|
|
|
let stderr = child.stderr.take().ok_or_else(|| {
|
|
|
|
ShellError::ExternalCommand(
|
|
|
|
"Error taking stderr from external".to_string(),
|
|
|
|
"Redirects need access to stderr of an external command"
|
|
|
|
.to_string(),
|
|
|
|
span,
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// Stderr is read using the Buffer reader. It will do so until there is an
|
|
|
|
// error or there are no more bytes to read
|
|
|
|
let mut buf_read = BufReader::with_capacity(OUTPUT_BUFFER_SIZE, stderr);
|
|
|
|
while let Ok(bytes) = buf_read.fill_buf() {
|
|
|
|
if bytes.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Cow generated from the function represents the conversion
|
|
|
|
// from bytes to String. If no replacements are required, then the
|
|
|
|
// borrowed value is a proper UTF-8 string. The Owned option represents
|
|
|
|
// a string where the values had to be replaced, thus marking it as bytes
|
|
|
|
let bytes = bytes.to_vec();
|
|
|
|
let length = bytes.len();
|
|
|
|
buf_read.consume(length);
|
|
|
|
|
|
|
|
if let Some(ctrlc) = &ctrlc {
|
|
|
|
if ctrlc.load(Ordering::SeqCst) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match stderr_tx.send(bytes) {
|
|
|
|
Ok(_) => continue,
|
|
|
|
Err(_) => break,
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 23:22:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if redirect_stdout {
|
2021-12-04 13:38:21 +01:00
|
|
|
let stdout = child.stdout.take().ok_or_else(|| {
|
|
|
|
ShellError::ExternalCommand(
|
|
|
|
"Error taking stdout from external".to_string(),
|
2022-01-22 15:12:34 +01:00
|
|
|
"Redirects need access to stdout of an external command"
|
|
|
|
.to_string(),
|
2021-12-04 13:38:21 +01:00
|
|
|
span,
|
|
|
|
)
|
|
|
|
})?;
|
2021-09-29 20:25:05 +02:00
|
|
|
|
|
|
|
// Stdout is read using the Buffer reader. It will do so until there is an
|
|
|
|
// error or there are no more bytes to read
|
|
|
|
let mut buf_read = BufReader::with_capacity(OUTPUT_BUFFER_SIZE, stdout);
|
|
|
|
while let Ok(bytes) = buf_read.fill_buf() {
|
|
|
|
if bytes.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Cow generated from the function represents the conversion
|
|
|
|
// from bytes to String. If no replacements are required, then the
|
|
|
|
// borrowed value is a proper UTF-8 string. The Owned option represents
|
|
|
|
// a string where the values had to be replaced, thus marking it as bytes
|
2021-12-24 08:22:11 +01:00
|
|
|
let bytes = bytes.to_vec();
|
2021-09-29 20:25:05 +02:00
|
|
|
let length = bytes.len();
|
|
|
|
buf_read.consume(length);
|
|
|
|
|
2021-11-09 07:14:00 +01:00
|
|
|
if let Some(ctrlc) = &ctrlc {
|
|
|
|
if ctrlc.load(Ordering::SeqCst) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 20:51:31 +01:00
|
|
|
match stdout_tx.send(bytes) {
|
2021-09-29 20:25:05 +02:00
|
|
|
Ok(_) => continue,
|
|
|
|
Err(_) => break,
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 07:14:00 +01:00
|
|
|
}
|
2021-09-29 20:25:05 +02:00
|
|
|
|
2021-11-09 07:14:00 +01:00
|
|
|
match child.wait() {
|
2022-01-22 15:12:34 +01:00
|
|
|
Err(err) => Err(ShellError::ExternalCommand(
|
|
|
|
"External command exited with error".into(),
|
|
|
|
err.to_string(),
|
|
|
|
span,
|
|
|
|
)),
|
2022-02-25 20:51:31 +01:00
|
|
|
Ok(x) => {
|
|
|
|
if let Some(code) = x.code() {
|
|
|
|
let _ = exit_code_tx.send(Value::Int {
|
|
|
|
val: code as i64,
|
|
|
|
span: head,
|
|
|
|
});
|
|
|
|
} else if x.success() {
|
|
|
|
let _ = exit_code_tx.send(Value::Int { val: 0, span: head });
|
|
|
|
} else {
|
|
|
|
let _ = exit_code_tx.send(Value::Int {
|
|
|
|
val: -1,
|
|
|
|
span: head,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-09 07:14:00 +01:00
|
|
|
}
|
|
|
|
});
|
2022-02-25 20:51:31 +01:00
|
|
|
let stdout_receiver = ChannelReceiver::new(stdout_rx);
|
|
|
|
let stderr_receiver = ChannelReceiver::new(stderr_rx);
|
|
|
|
let exit_code_receiver = ValueReceiver::new(exit_code_rx);
|
|
|
|
|
|
|
|
Ok(PipelineData::ExternalStream {
|
2022-03-08 02:17:33 +01:00
|
|
|
stdout: if redirect_stdout {
|
|
|
|
Some(RawStream::new(
|
|
|
|
Box::new(stdout_receiver),
|
|
|
|
output_ctrlc.clone(),
|
|
|
|
head,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
2022-02-25 20:51:31 +01:00
|
|
|
stderr: Some(RawStream::new(
|
|
|
|
Box::new(stderr_receiver),
|
|
|
|
output_ctrlc.clone(),
|
|
|
|
head,
|
|
|
|
)),
|
|
|
|
exit_code: Some(ListStream::from_stream(
|
|
|
|
Box::new(exit_code_receiver),
|
|
|
|
output_ctrlc,
|
|
|
|
)),
|
|
|
|
span: head,
|
|
|
|
metadata: None,
|
|
|
|
})
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 14:13:38 +01:00
|
|
|
fn create_process(
|
|
|
|
&self,
|
|
|
|
input: &PipelineData,
|
|
|
|
use_cmd: bool,
|
|
|
|
span: Span,
|
|
|
|
) -> Result<CommandSys, ShellError> {
|
|
|
|
let mut process = if let Some(d) = self.env_vars.get("PWD") {
|
|
|
|
let mut process = if use_cmd {
|
|
|
|
self.spawn_cmd_command()
|
|
|
|
} else {
|
|
|
|
self.create_command(d)?
|
|
|
|
};
|
|
|
|
|
|
|
|
process.current_dir(d);
|
|
|
|
process
|
|
|
|
} else {
|
2022-04-18 14:34:10 +02:00
|
|
|
return Err(ShellError::GenericError(
|
2022-02-22 14:13:38 +01:00
|
|
|
"Current directory not found".to_string(),
|
|
|
|
"did not find PWD environment variable".to_string(),
|
2022-04-18 14:34:10 +02:00
|
|
|
Some(span),
|
|
|
|
Some(concat!(
|
2022-02-22 14:13:38 +01:00
|
|
|
"The environment variable 'PWD' was not found. ",
|
|
|
|
"It is required to define the current directory when running an external command."
|
2022-04-18 14:34:10 +02:00
|
|
|
).to_string()),
|
|
|
|
Vec::new(),
|
2022-02-22 14:13:38 +01:00
|
|
|
));
|
|
|
|
};
|
|
|
|
|
|
|
|
process.envs(&self.env_vars);
|
|
|
|
|
|
|
|
// If the external is not the last command, its output will get piped
|
|
|
|
// either as a string or binary
|
|
|
|
if self.redirect_stdout {
|
|
|
|
process.stdout(Stdio::piped());
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.redirect_stderr {
|
|
|
|
process.stderr(Stdio::piped());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is an input from the pipeline. The stdin from the process
|
|
|
|
// is piped so it can be used to send the input information
|
|
|
|
if !matches!(input, PipelineData::Value(Value::Nothing { .. }, ..)) {
|
|
|
|
process.stdin(Stdio::piped());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(process)
|
|
|
|
}
|
|
|
|
|
2022-01-24 19:26:56 +01:00
|
|
|
fn create_command(&self, cwd: &str) -> Result<CommandSys, ShellError> {
|
2021-09-29 20:25:05 +02:00
|
|
|
// in all the other cases shell out
|
|
|
|
if cfg!(windows) {
|
|
|
|
//TODO. This should be modifiable from the config file.
|
|
|
|
// We could give the option to call from powershell
|
|
|
|
// for minimal builds cwd is unused
|
2021-12-02 21:55:16 +01:00
|
|
|
if self.name.item.ends_with(".cmd") || self.name.item.ends_with(".bat") {
|
2022-01-24 19:26:56 +01:00
|
|
|
Ok(self.spawn_cmd_command())
|
2021-12-02 21:55:16 +01:00
|
|
|
} else {
|
2022-01-05 01:26:01 +01:00
|
|
|
self.spawn_simple_command(cwd)
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
2021-12-02 21:55:16 +01:00
|
|
|
} else if self.name.item.ends_with(".sh") {
|
2022-01-24 19:26:56 +01:00
|
|
|
Ok(self.spawn_sh_command())
|
2021-09-29 20:25:05 +02:00
|
|
|
} else {
|
2022-01-05 01:26:01 +01:00
|
|
|
self.spawn_simple_command(cwd)
|
2021-12-02 21:55:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Spawn a command without shelling out to an external shell
|
2022-01-25 18:27:35 +01:00
|
|
|
pub fn spawn_simple_command(&self, cwd: &str) -> Result<std::process::Command, ShellError> {
|
2022-04-11 10:05:39 +02:00
|
|
|
let head = trim_enclosing_quotes(&self.name.item);
|
2022-04-16 22:05:42 +02:00
|
|
|
let head = nu_path::expand_to_real_path(head)
|
2022-04-13 20:42:57 +02:00
|
|
|
.to_string_lossy()
|
|
|
|
.to_string();
|
2022-04-11 10:05:39 +02:00
|
|
|
|
2022-01-31 18:42:12 +01:00
|
|
|
let mut process = std::process::Command::new(&head);
|
2021-12-02 21:55:16 +01:00
|
|
|
|
2022-01-24 19:26:56 +01:00
|
|
|
for arg in self.args.iter() {
|
2022-01-25 14:11:35 +01:00
|
|
|
let mut arg = Spanned {
|
2022-01-24 19:26:56 +01:00
|
|
|
item: trim_enclosing_quotes(&arg.item),
|
|
|
|
span: arg.span,
|
2022-01-05 02:09:53 +01:00
|
|
|
};
|
2022-04-16 22:05:42 +02:00
|
|
|
arg.item = nu_path::expand_to_real_path(arg.item)
|
2022-04-13 20:42:57 +02:00
|
|
|
.to_string_lossy()
|
|
|
|
.to_string();
|
2021-12-03 08:40:31 +01:00
|
|
|
|
2022-01-24 19:26:56 +01:00
|
|
|
let cwd = PathBuf::from(cwd);
|
2022-01-23 00:35:52 +01:00
|
|
|
|
2022-01-24 19:26:56 +01:00
|
|
|
if arg.item.contains('*') {
|
|
|
|
if let Ok((prefix, matches)) = nu_engine::glob_from(&arg, &cwd, self.name.span) {
|
|
|
|
let matches: Vec<_> = matches.collect();
|
2022-01-23 00:35:52 +01:00
|
|
|
|
2022-02-15 14:47:25 +01:00
|
|
|
// FIXME: do we want to special-case this further? We might accidentally expand when they don't
|
|
|
|
// intend to
|
2022-01-24 19:26:56 +01:00
|
|
|
if matches.is_empty() {
|
2022-02-15 14:47:25 +01:00
|
|
|
process.arg(&arg.item);
|
2022-01-24 19:26:56 +01:00
|
|
|
}
|
|
|
|
for m in matches {
|
|
|
|
if let Ok(arg) = m {
|
|
|
|
let arg = if let Some(prefix) = &prefix {
|
|
|
|
if let Ok(remainder) = arg.strip_prefix(&prefix) {
|
|
|
|
let new_prefix = if let Some(pfx) = diff_paths(&prefix, &cwd) {
|
|
|
|
pfx
|
|
|
|
} else {
|
|
|
|
prefix.to_path_buf()
|
|
|
|
};
|
|
|
|
|
|
|
|
new_prefix.join(remainder).to_string_lossy().to_string()
|
|
|
|
} else {
|
|
|
|
arg.to_string_lossy().to_string()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
arg.to_string_lossy().to_string()
|
|
|
|
};
|
|
|
|
|
2022-01-31 18:42:12 +01:00
|
|
|
process.arg(&arg);
|
2022-01-24 19:26:56 +01:00
|
|
|
} else {
|
2022-01-31 18:42:12 +01:00
|
|
|
process.arg(&arg.item);
|
2022-01-24 19:26:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-31 18:42:12 +01:00
|
|
|
process.arg(&arg.item);
|
2022-01-24 19:26:56 +01:00
|
|
|
}
|
2021-12-02 21:55:16 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 19:26:56 +01:00
|
|
|
Ok(process)
|
2021-12-02 21:55:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Spawn a cmd command with `cmd /c args...`
|
2022-01-25 18:27:35 +01:00
|
|
|
pub fn spawn_cmd_command(&self) -> std::process::Command {
|
2021-12-02 21:55:16 +01:00
|
|
|
let mut process = std::process::Command::new("cmd");
|
2022-03-23 07:05:06 +01:00
|
|
|
|
|
|
|
// Disable AutoRun
|
|
|
|
// TODO: There should be a config option to enable/disable this
|
|
|
|
// Alternatively (even better) a config option to specify all the arguments to pass to cmd
|
|
|
|
process.arg("/D");
|
|
|
|
|
2021-12-02 21:55:16 +01:00
|
|
|
process.arg("/c");
|
|
|
|
process.arg(&self.name.item);
|
|
|
|
for arg in &self.args {
|
|
|
|
// Clean the args before we use them:
|
|
|
|
// https://stackoverflow.com/questions/1200235/how-to-pass-a-quoted-pipe-character-to-cmd-exe
|
|
|
|
// cmd.exe needs to have a caret to escape a pipe
|
2022-02-24 20:02:28 +01:00
|
|
|
let arg = arg.item.replace('|', "^|");
|
2021-12-02 21:55:16 +01:00
|
|
|
process.arg(&arg);
|
|
|
|
}
|
|
|
|
process
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Spawn a sh command with `sh -c args...`
|
2022-01-25 18:27:35 +01:00
|
|
|
pub fn spawn_sh_command(&self) -> std::process::Command {
|
2022-01-24 19:26:56 +01:00
|
|
|
let joined_and_escaped_arguments = self
|
|
|
|
.args
|
|
|
|
.iter()
|
|
|
|
.map(|arg| shell_arg_escape(&arg.item))
|
|
|
|
.join(" ");
|
2021-12-02 21:55:16 +01:00
|
|
|
let cmd_with_args = vec![self.name.item.clone(), joined_and_escaped_arguments].join(" ");
|
|
|
|
let mut process = std::process::Command::new("sh");
|
|
|
|
process.arg("-c").arg(cmd_with_args);
|
|
|
|
process
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_unsafe_shell_characters(arg: &str) -> bool {
|
|
|
|
let re: Regex = Regex::new(r"[^\w@%+=:,./-]").expect("regex to be valid");
|
|
|
|
|
|
|
|
re.is_match(arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shell_arg_escape(arg: &str) -> String {
|
|
|
|
match arg {
|
|
|
|
"" => String::from("''"),
|
|
|
|
s if !has_unsafe_shell_characters(s) => String::from(s),
|
|
|
|
_ => {
|
|
|
|
let single_quotes_escaped = arg.split('\'').join("'\"'\"'");
|
|
|
|
format!("'{}'", single_quotes_escaped)
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 07:15:23 +01:00
|
|
|
fn trim_enclosing_quotes(input: &str) -> String {
|
|
|
|
let mut chars = input.chars();
|
|
|
|
|
|
|
|
match (chars.next(), chars.next_back()) {
|
|
|
|
(Some('"'), Some('"')) => chars.collect(),
|
|
|
|
(Some('\''), Some('\'')) => chars.collect(),
|
|
|
|
_ => input.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 20:51:31 +01:00
|
|
|
// Receiver used for the RawStream
|
|
|
|
// It implements iterator so it can be used as a RawStream
|
2021-09-29 20:25:05 +02:00
|
|
|
struct ChannelReceiver {
|
2021-12-24 08:22:11 +01:00
|
|
|
rx: mpsc::Receiver<Vec<u8>>,
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ChannelReceiver {
|
2021-12-24 08:22:11 +01:00
|
|
|
pub fn new(rx: mpsc::Receiver<Vec<u8>>) -> Self {
|
|
|
|
Self { rx }
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for ChannelReceiver {
|
2021-12-24 20:24:55 +01:00
|
|
|
type Item = Result<Vec<u8>, ShellError>;
|
2021-09-29 20:25:05 +02:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
match self.rx.recv() {
|
2021-12-24 20:24:55 +01:00
|
|
|
Ok(v) => Some(Ok(v)),
|
2021-09-29 20:25:05 +02:00
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-25 20:51:31 +01:00
|
|
|
|
|
|
|
// Receiver used for the ListStream
|
|
|
|
// It implements iterator so it can be used as a ListStream
|
|
|
|
struct ValueReceiver {
|
|
|
|
rx: mpsc::Receiver<Value>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ValueReceiver {
|
|
|
|
pub fn new(rx: mpsc::Receiver<Value>) -> Self {
|
|
|
|
Self { rx }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for ValueReceiver {
|
|
|
|
type Item = Value;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
match self.rx.recv() {
|
|
|
|
Ok(v) => Some(v),
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|