-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathcmd.rs
32 lines (28 loc) · 968 Bytes
/
cmd.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use anyhow::anyhow;
use anyhow::bail;
use anyhow::Result;
use std::path::PathBuf;
use std::process::Command;
use which::which;
/// Runs the command with the given arguments and returns its stdout if the command
/// exits successfully. If the command fails, returns an error.
pub(crate) fn run_command(cmd: &str, args: &[&str]) -> Result<String> {
let output = Command::new(cmd).args(args).output()?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
bail!("{}", stderr);
}
let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8");
Ok(stdout)
}
pub(crate) fn find_executable(name: &str, error_msg: &str) -> Result<PathBuf> {
let path = which(name).map_err(|_| {
anyhow!(
"The `{}` executable could not be found in your PATH. {}",
name,
error_msg
)
})?;
info!("Found {} executable at {:?}", name, path);
Ok(path)
}