2022-12-21 22:33:26 +00:00
|
|
|
use super::run_external::create_external_command;
|
2023-11-08 20:50:25 +00:00
|
|
|
use nu_engine::current_dir;
|
2022-01-25 17:27:35 +00:00
|
|
|
use nu_protocol::{
|
2022-12-21 22:33:26 +00:00
|
|
|
ast::Call,
|
2022-01-25 17:27:35 +00:00
|
|
|
engine::{Command, EngineState, Stack},
|
2023-11-08 20:50:25 +00:00
|
|
|
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
|
2022-01-25 17:27:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Exec;
|
|
|
|
|
|
|
|
impl Command for Exec {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"exec"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("exec")
|
2022-12-21 19:20:46 +00:00
|
|
|
.input_output_types(vec![(Type::Nothing, Type::Any)])
|
2022-01-25 17:27:35 +00:00
|
|
|
.required("command", SyntaxShape::String, "the command to execute")
|
2022-12-21 22:33:26 +00:00
|
|
|
.allows_unknown_args()
|
2022-01-25 17:27:35 +00:00
|
|
|
.category(Category::System)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2023-11-08 20:50:25 +00:00
|
|
|
"Execute a command, replacing or exiting the current process, depending on platform."
|
2022-01-25 17:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extra_usage(&self) -> &str {
|
2023-11-08 20:50:25 +00:00
|
|
|
r#"On Unix-based systems, the current process is replaced with the command.
|
|
|
|
On Windows based systems, Nushell will wait for the command to finish and then exit with the command's exit code."#
|
2022-01-25 17:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
_input: PipelineData,
|
2023-02-05 21:17:46 +00:00
|
|
|
) -> Result<PipelineData, ShellError> {
|
2022-01-25 17:27:35 +00:00
|
|
|
exec(engine_state, stack, call)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Execute external 'ps aux' tool",
|
|
|
|
example: "exec ps aux",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Execute 'nautilus'",
|
|
|
|
example: "exec nautilus",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exec(
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
2023-02-05 21:17:46 +00:00
|
|
|
) -> Result<PipelineData, ShellError> {
|
2023-11-08 20:50:25 +00:00
|
|
|
let external_command =
|
|
|
|
create_external_command(engine_state, stack, call, false, false, false, false)?;
|
2022-01-25 17:27:35 +00:00
|
|
|
|
2022-12-21 22:33:26 +00:00
|
|
|
let cwd = current_dir(engine_state, stack)?;
|
2022-06-04 06:47:36 +00:00
|
|
|
let mut command = external_command.spawn_simple_command(&cwd.to_string_lossy())?;
|
2022-12-21 22:33:26 +00:00
|
|
|
command.current_dir(cwd);
|
2023-11-08 20:50:25 +00:00
|
|
|
command.envs(external_command.env_vars);
|
|
|
|
|
|
|
|
// this either replaces our process and should not return,
|
|
|
|
// or the exec fails and we get an error back
|
|
|
|
exec_impl(command, call.head)
|
|
|
|
}
|
2022-01-25 17:27:35 +00:00
|
|
|
|
2023-11-08 20:50:25 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn exec_impl(mut command: std::process::Command, span: Span) -> Result<PipelineData, ShellError> {
|
|
|
|
use std::os::unix::process::CommandExt;
|
|
|
|
|
|
|
|
let error = command.exec();
|
2022-01-25 17:27:35 +00:00
|
|
|
|
2023-12-06 23:40:03 +00:00
|
|
|
Err(ShellError::GenericError {
|
|
|
|
error: "Error on exec".into(),
|
|
|
|
msg: error.to_string(),
|
|
|
|
span: Some(span),
|
|
|
|
help: None,
|
|
|
|
inner: vec![],
|
|
|
|
})
|
2022-01-25 17:27:35 +00:00
|
|
|
}
|
2023-11-08 20:50:25 +00:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn exec_impl(mut command: std::process::Command, span: Span) -> Result<PipelineData, ShellError> {
|
|
|
|
match command.spawn() {
|
|
|
|
Ok(mut child) => match child.wait() {
|
|
|
|
Ok(status) => std::process::exit(status.code().unwrap_or(0)),
|
|
|
|
Err(e) => Err(ShellError::ExternalCommand {
|
|
|
|
label: "Error in external command".into(),
|
|
|
|
help: e.to_string(),
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Err(e) => Err(ShellError::ExternalCommand {
|
|
|
|
label: "Error spawning external command".into(),
|
|
|
|
help: e.to_string(),
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|