remove repeated function (#4600)

* remove repeated function

* name in signature
This commit is contained in:
Fernando Herrera 2022-02-22 13:13:38 +00:00 committed by GitHub
parent b448d1dbe1
commit c2bad71123
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 69 deletions

2
Cargo.lock generated
View File

@ -3313,7 +3313,7 @@ dependencies = [
[[package]] [[package]]
name = "reedline" name = "reedline"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/nushell/reedline?branch=main#e87e9d1fd3d392308975fb6f89c1a67cc43bc8b9" source = "git+https://github.com/nushell/reedline?branch=main#2db0ffd1c48ec1c03c538822900a368b40eec0a8"
dependencies = [ dependencies = [
"chrono", "chrono",
"crossterm", "crossterm",

View File

@ -35,7 +35,7 @@ impl Command for External {
} }
fn signature(&self) -> nu_protocol::Signature { fn signature(&self) -> nu_protocol::Signature {
Signature::build("run-external") Signature::build(self.name())
.switch("redirect-stdout", "redirect-stdout", None) .switch("redirect-stdout", "redirect-stdout", None)
.switch("redirect-stderr", "redirect-stderr", None) .switch("redirect-stderr", "redirect-stderr", None)
.rest("rest", SyntaxShape::Any, "external command to run") .rest("rest", SyntaxShape::Any, "external command to run")
@ -122,79 +122,14 @@ impl ExternalCommand {
let ctrlc = engine_state.ctrlc.clone(); let ctrlc = engine_state.ctrlc.clone();
let mut process = if let Some(d) = self.env_vars.get("PWD") { let mut process = self.create_process(&input, false, head)?;
let mut process = self.create_command(d)?;
process.current_dir(d);
process
} else {
return Err(ShellError::SpannedLabeledErrorHelp(
"Current directory not found".to_string(),
"did not find PWD environment variable".to_string(),
head,
concat!(
"The environment variable 'PWD' was not found. ",
"It is required to define the current directory when running an external command."
).to_string(),
));
};
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());
}
let child; let child;
#[cfg(windows)] #[cfg(windows)]
{ {
match process.spawn() { match process.spawn() {
Err(_) => { Err(_) => {
let mut process = self.spawn_cmd_command(); let mut process = self.create_process(&input, true, head)?;
if let Some(d) = self.env_vars.get("PWD") {
process.current_dir(d);
} else {
return Err(ShellError::SpannedLabeledErrorHelp(
"Current directory not found".to_string(),
"did not find PWD environment variable".to_string(),
head,
concat!(
"The environment variable 'PWD' was not found. ",
"It is required to define the current directory when running an external command."
).to_string(),
));
};
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());
}
child = process.spawn(); child = process.spawn();
} }
Ok(process) => { Ok(process) => {
@ -326,6 +261,54 @@ impl ExternalCommand {
} }
} }
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 {
return Err(ShellError::SpannedLabeledErrorHelp(
"Current directory not found".to_string(),
"did not find PWD environment variable".to_string(),
span,
concat!(
"The environment variable 'PWD' was not found. ",
"It is required to define the current directory when running an external command."
).to_string(),
));
};
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)
}
fn create_command(&self, cwd: &str) -> Result<CommandSys, ShellError> { fn create_command(&self, cwd: &str) -> Result<CommandSys, ShellError> {
// in all the other cases shell out // in all the other cases shell out
if cfg!(windows) { if cfg!(windows) {