Skip to content

Commit

Permalink
Simplify shelling out helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Jun 16, 2023
1 parent 31508f9 commit 5d58aa3
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

use std::process::Command;

/// Execute the CLI command specified. If the command succeeds,
/// returns stdout.
/// Execute the CLI command specified.
///
/// # Failures
///
/// If the command isn't on $PATH, returns Err with a helpful
/// message. If the command returns a non-zero exit code, returns Err
/// with stderr.
fn shell_command(command: &str, args: &[&str]) -> Result<String, String> {
pub fn run_shell_command(command: &str, args: &[&str]) -> Result<(), String> {
let mut c = Command::new(command);
for arg in args {
c.arg(arg);
Expand All @@ -20,8 +19,7 @@ fn shell_command(command: &str, args: &[&str]) -> Result<String, String> {
match c.output() {
Ok(result) => {
if result.status.success() {
let stdout = String::from_utf8_lossy(&result.stdout);
Ok((*stdout).to_owned())
Ok(())
} else {
let stderr = String::from_utf8_lossy(&result.stderr);
Err((*stderr).to_owned())
Expand All @@ -30,11 +28,3 @@ fn shell_command(command: &str, args: &[&str]) -> Result<String, String> {
Err(_) => Err(format!("Could not execute '{}'. Is it on $PATH?", command)),
}
}

/// Execute a CLI command as `shell_command`, but ignore stdout.
pub fn run_shell_command(command: &str, args: &[&str]) -> Result<(), String> {
match shell_command(command, args) {
Ok(_) => Ok(()),
Err(e) => Err(e),
}
}

0 comments on commit 5d58aa3

Please sign in to comment.