forked from brl/citadel-tools
If command not absolute path, confirm it exists by searching $PATH
This commit is contained in:
parent
9f6ba7e619
commit
b7b27b039c
@ -1,19 +1,35 @@
|
|||||||
use std::path::Path;
|
use std::path::{Path,PathBuf};
|
||||||
use std::process::{Command,ExitStatus,Stdio};
|
use std::process::{Command,ExitStatus,Stdio};
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use libc::{self, c_char};
|
use libc::{self, c_char};
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::str::from_utf8_unchecked;
|
use std::str::from_utf8_unchecked;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
use failure::ResultExt;
|
use failure::ResultExt;
|
||||||
|
|
||||||
use Result;
|
use Result;
|
||||||
|
|
||||||
pub fn ensure_command_exists(cmd_path: &str) -> Result<()> {
|
fn search_path(filename: &str) -> Result<PathBuf> {
|
||||||
if !Path::new(cmd_path).exists() {
|
let path_var = env::var("PATH")?;
|
||||||
bail!("Cannot execute '{}': command does not exist", cmd_path);
|
for mut path in env::split_paths(&path_var) {
|
||||||
|
path.push(filename);
|
||||||
|
if path.exists() {
|
||||||
|
return Ok(path);
|
||||||
}
|
}
|
||||||
Ok(())
|
}
|
||||||
|
Err(format_err!("Could not find {} in $PATH", filename))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ensure_command_exists(cmd: &str) -> Result<()> {
|
||||||
|
let path = Path::new(cmd);
|
||||||
|
if !path.is_absolute() {
|
||||||
|
search_path(cmd)?;
|
||||||
|
return Ok(())
|
||||||
|
} else if path.exists() {
|
||||||
|
return Ok(())
|
||||||
|
}
|
||||||
|
Err(format_err!("Cannot execute '{}': command does not exist", cmd))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exec_cmdline<S: AsRef<str>>(cmd_path: &str, args: S) -> Result<()> {
|
pub fn exec_cmdline<S: AsRef<str>>(cmd_path: &str, args: S) -> Result<()> {
|
||||||
|
Loading…
Reference in New Issue
Block a user