Skip to content

Commit 2a429e5

Browse files
committed
fix: improve commands on windows to return stdout and stderr
1 parent 282e75f commit 2a429e5

File tree

1 file changed

+47
-49
lines changed

1 file changed

+47
-49
lines changed

postgresql_commands/src/traits.rs

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
use std::env;
12
use crate::error::{Error, Result};
23
use std::ffi::{OsStr, OsString};
34
use std::fmt::Debug;
45
use std::path::PathBuf;
6+
use std::process::ExitStatus;
57
use std::time::Duration;
68
use tracing::debug;
79

@@ -140,46 +142,41 @@ impl CommandExecutor for std::process::Command {
140142
/// Execute the command and return the stdout and stderr
141143
fn execute(&mut self) -> Result<(String, String)> {
142144
debug!("Executing command: {}", self.to_command_string());
143-
#[cfg(not(target_os = "windows"))]
144-
{
145-
let output = self.output()?;
146-
let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
147-
let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
148-
149-
debug!(
150-
"Result: {}\nstdout: {}\nstderr: {}",
151-
output
152-
.status
153-
.code()
154-
.map_or("None".to_string(), |c| c.to_string()),
155-
stdout,
156-
stderr
157-
);
158-
159-
if output.status.success() {
160-
Ok((stdout, stderr))
161-
} else {
162-
Err(Error::CommandError { stdout, stderr })
163-
}
164-
}
165-
166-
// TODO: Processes can hang on Windows when attempting to get stdout/stderr using code
167-
// that works for Linux/MacOS; this implementation should be updated to retrieve the
168-
// values of stdout/stderr without hanging
169-
#[cfg(target_os = "windows")]
170-
{
145+
let program = self.get_program().to_string_lossy().to_string();
146+
let stdout: String;
147+
let stderr: String;
148+
let status: ExitStatus;
149+
150+
if env::consts::OS == "windows" && program.as_str().ends_with("pg_ctl") {
151+
// TODO: Processes can hang on Windows when attempting to get stdout/stderr using code
152+
// that works for Linux/MacOS; this implementation should be updated to retrieve the
153+
// values of stdout/stderr without hanging
171154
let mut process = self
172155
.stdout(std::process::Stdio::piped())
173156
.stderr(std::process::Stdio::piped())
174157
.spawn()?;
175-
let status = process.wait()?;
176-
let stdout = String::new();
177-
let stderr = String::new();
178-
if status.success() {
179-
Ok((stdout, stderr))
180-
} else {
181-
Err(Error::CommandError { stdout, stderr })
182-
}
158+
stdout = String::new();
159+
stderr = String::new();
160+
status = process.wait()?;
161+
} else {
162+
let output = self.output()?;
163+
stdout = String::from_utf8_lossy(&output.stdout).into_owned();
164+
stderr = String::from_utf8_lossy(&output.stderr).into_owned();
165+
status = output.status;
166+
}
167+
debug!(
168+
"Result: {}\nstdout: {}\nstderr: {}",
169+
status
170+
.code()
171+
.map_or("None".to_string(), |c| c.to_string()),
172+
stdout,
173+
stderr
174+
);
175+
176+
if status.success() {
177+
Ok((stdout, stderr))
178+
} else {
179+
Err(Error::CommandError { stdout, stderr })
183180
}
184181
}
185182
}
@@ -194,19 +191,20 @@ impl AsyncCommandExecutor for tokio::process::Command {
194191
Some(duration) => tokio::time::timeout(duration, self.output()).await?,
195192
None => self.output().await,
196193
}?;
197-
198-
#[cfg(not(target_os = "windows"))]
199-
let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
200-
#[cfg(not(target_os = "windows"))]
201-
let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
202-
203-
// TODO: Processes can hang on Windows when attempting to get stdout/stderr using code
204-
// that works for Linux/MacOS; this implementation should be updated to retrieve the
205-
// values of stdout/stderr without hanging
206-
#[cfg(target_os = "windows")]
207-
let stdout = String::new();
208-
#[cfg(target_os = "windows")]
209-
let stderr = String::new();
194+
let program = self.get_program().to_string_lossy().to_string();
195+
let stdout: String;
196+
let stderr: String;
197+
198+
if std::env::OS == "windows" && program.as_str().ends_with("pg_ctl") {
199+
// TODO: Processes can hang on Windows when attempting to get stdout/stderr using code
200+
// that works for Linux/MacOS; this implementation should be updated to retrieve the
201+
// values of stdout/stderr without hanging
202+
stdout = String::new();
203+
stderr = String::new();
204+
} else {
205+
stdout = String::from_utf8_lossy(&output.stdout).into_owned();
206+
stderr = String::from_utf8_lossy(&output.stderr).into_owned();
207+
}
210208

211209
debug!(
212210
"Result: {}\nstdout: {}\nstderr: {}",

0 commit comments

Comments
 (0)