Skip to content

Commit

Permalink
Fix utils/execute.rs windows support
Browse files Browse the repository at this point in the history
Fix utils/execute.rs windows support so that on windows when we don't
have signals to worry about, we don't. But when we are on unix systems
that do have signals we do handle them.

This relates to issue #235.

Signed-off-by: Drew De Ponte <cyphactor@gmail.com>

[changelog]
fixed: execute external command support for windows

<!-- ps-id: 242e7637-266a-4bd8-b47e-359d6dd40dbc -->
  • Loading branch information
Alizter authored and drewdeponte committed Oct 13, 2023
1 parent c1fa142 commit e52a409
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/ps/private/utils/execute.rs
Expand Up @@ -3,7 +3,7 @@ use std::io;
use std::os::unix::process::ExitStatusExt;
#[cfg(target_family = "windows")]
use std::os::windows::process::ExitStatusExt;
use std::process::{Command, Output};
use std::process::{Command, ExitStatus, Output};
use std::result::Result;

#[derive(Debug)]
Expand All @@ -15,6 +15,19 @@ pub enum ExecuteError {
ExitMissingSignal, // triggered when we understand exit to be triggered by signal but no signal found
}

#[cfg(target_family = "unix")]
fn handle_error_no_code(status: ExitStatus) -> ExecuteError {
match status.signal() {
Some(signal) => ExecuteError::ExitSignal(signal),
None => ExecuteError::ExitMissingSignal,
}
}

#[cfg(target_family = "windows")]
fn handle_error_no_code(_: ExitStatus) -> ExecuteError {
return ExecuteError::ExitMissingSignal;
}

/// Execute an external command in the foreground allowing it to take over the
/// terminal while waiting for the external application to complete with an
/// exit status.
Expand All @@ -27,13 +40,10 @@ pub fn execute(exe: &str, args: &[&str]) -> Result<(), ExecuteError> {
if status.success() {
Ok(())
} else {
match status.code() {
Some(code) => Err(ExecuteError::ExitStatus(code)),
None => match status.signal() {
Some(signal) => Err(ExecuteError::ExitSignal(signal)),
None => Err(ExecuteError::ExitMissingSignal),
},
}
Err(match status.code() {
Some(code) => ExecuteError::ExitStatus(code),
None => handle_error_no_code(status),
})
}
}
},
Expand Down

0 comments on commit e52a409

Please sign in to comment.