Skip to content

Commit

Permalink
feat: add option to suppress stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Jan 11, 2024
1 parent 204edfa commit db7d7db
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/ansible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl AnsibleRunnerInterface for AnsibleRunner {
"--list".to_string(),
],
true,
false,
)?;

// Debug the output of the inventory list.
Expand Down Expand Up @@ -175,6 +176,7 @@ impl AnsibleRunnerInterface for AnsibleRunner {
self.working_directory_path.clone(),
args,
false,
false,
)?;
Ok(())
}
Expand Down
20 changes: 11 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,8 @@ pub fn run_external_command(
binary_path: PathBuf,
working_directory_path: PathBuf,
args: Vec<String>,
suppress_output: bool,
suppress_stdout: bool,
suppress_stderr: bool,
) -> Result<Vec<String>> {
let mut command = Command::new(binary_path.clone());
for arg in &args {
Expand All @@ -635,27 +636,28 @@ pub fn run_external_command(
debug!("Working directory set to {working_directory_path:#?}");

let mut child = command.spawn()?;
let mut output_lines = Vec::new();
let mut stdout_lines = Vec::new();
let mut stderr_lines = Vec::new();

if let Some(ref mut stdout) = child.stdout {
let reader = BufReader::new(stdout);
for line in reader.lines() {
let line = line?;
if !suppress_output {
println!("{}", &line);
if !suppress_stdout {
println!("{line}");
}
output_lines.push(line);
stdout_lines.push(line);
}
}

if let Some(ref mut stderr) = child.stderr {
let reader = BufReader::new(stderr);
for line in reader.lines() {
let line = line?;
if !suppress_output {
println!("{}", &line);
if !suppress_stderr {
eprintln!("{line}");
}
output_lines.push(line);
stderr_lines.push(line);
}
}

Expand All @@ -666,7 +668,7 @@ pub fn run_external_command(
return Err(Error::ExternalCommandRunFailed(binary_path.to_string()));
}

Ok(output_lines)
Ok(stdout_lines)
}

pub fn is_binary_on_path(binary_name: &str) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl TestnetDeploy {
PathBuf::from("."),
vec!["-R".to_string(), format!("{ip_address}")],
false,
false,
) {
println!("Failed to ssh-keygen {vm_name:?} : {ip_address} with err: {err:?}");
} else if let Err(err) =
Expand Down Expand Up @@ -118,6 +119,7 @@ impl TestnetDeploy {
PathBuf::from("."),
rsync_args_clone.clone(),
true,
false,
)?;

debug!("Finished rsync for for {vm_name:?} : {ip_address}");
Expand Down
1 change: 1 addition & 0 deletions src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl RpcClientInterface for RpcClient {
self.working_directory_path.clone(),
vec![rpc_address.to_string(), "info".to_string()],
false,
false,
)?;

let endpoint = output
Expand Down
3 changes: 3 additions & 0 deletions src/safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl SafeClientInterface for SafeClient {
"download".to_string(),
],
false,
false,
)?;
Ok(())
}
Expand All @@ -67,6 +68,7 @@ impl SafeClientInterface for SafeClient {
path.to_string_lossy().to_string(),
],
false,
false,
)?;

let re = Regex::new(r"Uploaded .+ to ([a-fA-F0-9]+)")?;
Expand All @@ -93,6 +95,7 @@ impl SafeClientInterface for SafeClient {
faucet_addr.to_string(),
],
false,
false,
)?;
Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions src/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl SshClientInterface for SshClient {
"--version".to_string(),
],
false,
false,
);
if result.is_ok() {
println!("SSH is available.");
Expand Down Expand Up @@ -122,6 +123,7 @@ impl SshClientInterface for SshClient {
std::env::current_dir()?,
args,
suppress_output,
false,
)
.map_err(|_| Error::SshCommandFailed(command.to_string()))?;
Ok(output)
Expand Down Expand Up @@ -159,6 +161,7 @@ impl SshClientInterface for SshClient {
std::env::current_dir()?,
args,
suppress_output,
false,
)
.map_err(|e| {
Error::SshCommandFailed(format!(
Expand All @@ -185,6 +188,7 @@ impl SshClientInterface for SshClient {
std::env::current_dir()?,
args,
suppress_output,
false,
)
.map_err(|e| {
Error::SshCommandFailed(format!("Failed to execute command on remote host: {e}"))
Expand Down
7 changes: 7 additions & 0 deletions src/terraform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl TerraformRunnerInterface for TerraformRunner {
self.working_directory_path.clone(),
args,
false,
false,
)?;
Ok(())
}
Expand All @@ -57,6 +58,7 @@ impl TerraformRunnerInterface for TerraformRunner {
self.working_directory_path.clone(),
vec!["destroy".to_string(), "-auto-approve".to_string()],
false,
false,
)?;
Ok(())
}
Expand All @@ -72,6 +74,7 @@ impl TerraformRunnerInterface for TerraformRunner {
self.working_directory_path.clone(),
args,
false,
false,
)?;
Ok(())
}
Expand All @@ -86,6 +89,7 @@ impl TerraformRunnerInterface for TerraformRunner {
name.to_string(),
],
true,
false,
)?;
Ok(())
}
Expand All @@ -96,6 +100,7 @@ impl TerraformRunnerInterface for TerraformRunner {
self.working_directory_path.clone(),
vec!["workspace".to_string(), "list".to_string()],
true,
false,
)?;
let workspaces: Vec<String> = output
.into_iter()
Expand All @@ -111,6 +116,7 @@ impl TerraformRunnerInterface for TerraformRunner {
self.working_directory_path.clone(),
vec!["workspace".to_string(), "new".to_string(), name.to_string()],
false,
false,
)?;
Ok(())
}
Expand All @@ -125,6 +131,7 @@ impl TerraformRunnerInterface for TerraformRunner {
name.to_string(),
],
false,
false,
)?;
Ok(())
}
Expand Down

0 comments on commit db7d7db

Please sign in to comment.