From 245693be1bd9650daf292937813a524dbcef8eaa Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 21 Jan 2023 21:35:43 +0100 Subject: [PATCH] Add executable path adjustments for windows --- src/mapper/mapped_command.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/mapper/mapped_command.rs b/src/mapper/mapped_command.rs index 5bb6e9a..0784925 100644 --- a/src/mapper/mapped_command.rs +++ b/src/mapper/mapped_command.rs @@ -29,10 +29,8 @@ impl MappedCommand { } #[tracing::instrument(skip_all, level = "debug")] - pub async fn run(self) -> CommandResult { - if !self.path.exists() { - return Err(CommandError::NotFound(self.path)); - } + pub async fn run(mut self) -> CommandResult { + self.adjust_path()?; let exit_status = Command::new(self.path) .args(self.args) .stdin(Stdio::inherit()) @@ -44,4 +42,31 @@ impl MappedCommand { Ok(exit_status) } + + #[cfg(not(target_os = "windows"))] + fn adjust_path(&mut self) -> CommandResult<()> { + if !self.path.exists() { + Err(CommandError::NotFound(self.path.to_owned())) + } else { + Ok(()) + } + } + + #[cfg(target_os = "windows")] + fn adjust_path(&mut self) -> CommandResult<()> { + if !self.path.exists() { + let extensions = ["exe", "bat", "cmd", "ps1"]; + for extension in &extensions { + let joined_path = self.path.with_extension(extension); + + if joined_path.exists() { + self.path = joined_path; + return Ok(()); + } + } + return Err(CommandError::NotFound(self.path.to_owned())); + } + + Ok(()) + } }