refactor: allow passing OsStr as-is to exec_cmd (#2997)

This commit is contained in:
David Knaack
2021-08-23 18:49:30 +02:00
committed by GitHub
parent 370cf92d79
commit 9d3ec93d82
4 changed files with 68 additions and 38 deletions
+9 -7
View File
@@ -9,7 +9,8 @@ use git2::{ErrorCode::UnbornBranch, Repository, RepositoryState};
use once_cell::sync::OnceCell;
use std::collections::{HashMap, HashSet};
use std::env;
use std::ffi::OsString;
use std::ffi::{OsStr, OsString};
use std::fmt::Debug;
use std::fs;
use std::path::{Path, PathBuf};
use std::string::String;
@@ -266,18 +267,19 @@ impl<'a> Context<'a> {
/// Execute a command and return the output on stdout and stderr if successful
#[inline]
pub fn exec_cmd(&self, cmd: &str, args: &[&str]) -> Option<CommandOutput> {
pub fn exec_cmd<T: AsRef<OsStr> + Debug, U: AsRef<OsStr> + Debug>(
&self,
cmd: T,
args: &[U],
) -> Option<CommandOutput> {
#[cfg(test)]
{
let command = match args.len() {
0 => cmd.to_owned(),
_ => format!("{} {}", cmd, args.join(" ")),
};
let command = crate::utils::display_command(&cmd, args);
if let Some(output) = self.cmd.get(command.as_str()) {
return output.clone();
}
}
exec_cmd(cmd, args, self.cmd_timeout)
exec_cmd(&cmd, args, self.cmd_timeout)
}
}