2022-12-21 22:33:26 +00:00
|
|
|
use super::run_external::create_external_command;
|
|
|
|
use nu_engine::{current_dir, CallExt};
|
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},
|
2022-12-21 19:20:46 +00:00
|
|
|
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type,
|
2022-01-25 17:27:35 +00:00
|
|
|
};
|
2022-10-07 18:54:36 +00:00
|
|
|
use std::os::unix::process::CommandExt;
|
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 {
|
|
|
|
"Execute a command, replacing the current process."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extra_usage(&self) -> &str {
|
|
|
|
"Currently supported only on Unix-based systems."
|
|
|
|
}
|
|
|
|
|
|
|
|
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> {
|
2022-01-25 17:27:35 +00:00
|
|
|
let name: Spanned<String> = call.req(engine_state, stack, 0)?;
|
|
|
|
let name_span = name.span;
|
|
|
|
|
2022-12-21 22:33:26 +00:00
|
|
|
let redirect_stdout = call.has_flag("redirect-stdout");
|
|
|
|
let redirect_stderr = call.has_flag("redirect-stderr");
|
2023-04-28 12:55:48 +00:00
|
|
|
let redirect_combine = call.has_flag("redirect-combine");
|
2022-12-21 22:33:26 +00:00
|
|
|
let trim_end_newline = call.has_flag("trim-end-newline");
|
2022-01-25 17:27:35 +00:00
|
|
|
|
2022-12-21 22:33:26 +00:00
|
|
|
let external_command = create_external_command(
|
|
|
|
engine_state,
|
|
|
|
stack,
|
|
|
|
call,
|
|
|
|
redirect_stdout,
|
|
|
|
redirect_stderr,
|
2023-04-28 12:55:48 +00:00
|
|
|
redirect_combine,
|
2022-12-21 22:33:26 +00:00
|
|
|
trim_end_newline,
|
|
|
|
)?;
|
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-04-20 10:10:46 +00:00
|
|
|
command.envs(&external_command.env_vars);
|
2022-01-25 17:27:35 +00:00
|
|
|
|
|
|
|
let err = command.exec(); // this replaces our process, should not return
|
|
|
|
|
2022-04-18 12:34:10 +00:00
|
|
|
Err(ShellError::GenericError(
|
2022-01-25 17:27:35 +00:00
|
|
|
"Error on exec".to_string(),
|
|
|
|
err.to_string(),
|
2022-04-18 12:34:10 +00:00
|
|
|
Some(name_span),
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
2022-01-25 17:27:35 +00:00
|
|
|
))
|
|
|
|
}
|