#[cfg(windows)]
use super::windows::{
resolve_cmd_exe, resolve_windows_program, windows_quote_for_cmd, windows_requires_cmd_shell,
};
use crate::SubprocessError;
pub(crate) fn resolve_command_invocation(
program: &str,
args: &[&str],
) -> Result<(String, Vec<String>), SubprocessError> {
#[cfg(windows)]
{
let resolved_program = resolve_windows_program(program).ok_or_else(|| {
SubprocessError::new(format!(
"command not found in any absolute PATH directory \
(current directory excluded for security): {program}"
))
})?;
if windows_requires_cmd_shell(&resolved_program) {
let cmd_exe = resolve_cmd_exe().ok_or_else(|| {
SubprocessError::new(format!(
"cmd.exe not found via %SystemRoot%\\System32 or %ComSpec%; \
refusing to execute batch wrapper via a CWD-searchable bare \
name: {resolved_program}"
))
})?;
let command_line = std::iter::once(resolved_program.as_str())
.chain(args.iter().copied())
.map(windows_quote_for_cmd)
.collect::<Vec<_>>()
.join(" ");
let shell_args = vec![
"/D".to_string(),
"/V:OFF".to_string(),
"/S".to_string(),
"/C".to_string(),
command_line,
];
return Ok((cmd_exe, shell_args));
}
Ok((resolved_program, args.iter().map(|arg| (*arg).to_string()).collect()))
}
#[cfg(not(windows))]
{
Ok((program.to_string(), args.iter().map(|arg| (*arg).to_string()).collect()))
}
}