cmd_exists/
lib.rs

1use std::{
2    io::{ErrorKind, Result},
3    process::{Command, Stdio},
4};
5
6/// Determine if the provided program exists.
7pub fn cmd_exists<T: AsRef<str>>(program: T) -> Result<()> {
8    let mut cmd = Command::new(if cfg!(windows) { "where" } else { "sh" });
9
10    let arg = {
11        #[cfg(target_os = "linux")]
12        {
13            cmd.arg("-c"); // invocation flag
14            format!("command -v {}", program.as_ref())
15        }
16
17        #[cfg(not(target_os = "linux"))]
18        program.as_ref()
19    };
20
21    if cmd
22        .arg(arg)
23        .stdout(Stdio::null())
24        .stderr(Stdio::null())
25        .status()?
26        .success()
27    {
28        Ok(())
29    } else {
30        Err(ErrorKind::NotFound.into())
31    }
32}