use anyhow::Result;
use std::process::{Command, Stdio};
use crate::config::ShimConfig;
use crate::platform::PlatformExecutor;
pub struct Executor {
config: ShimConfig,
platform: PlatformExecutor,
}
impl Executor {
pub fn new(config: ShimConfig) -> Self {
Self {
config,
platform: PlatformExecutor::new(),
}
}
pub fn execute(&self, args: &[String]) -> Result<i32> {
let target_path = self.config.resolved_path();
let mut cmd_args = self.config.resolved_args();
cmd_args.extend_from_slice(args);
let mut command = Command::new(&target_path);
command.args(&cmd_args);
if let Some(working_dir) = self.config.resolved_working_dir() {
command.current_dir(working_dir);
}
for (key, value) in self.config.resolved_env() {
command.env(key, value);
}
command
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
self.platform.execute(command, &self.config)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::ShimConfig;
use std::collections::HashMap;
#[test]
fn test_executor_creation() {
let config = ShimConfig {
path: "/bin/echo".to_string(),
args: Some(vec!["hello".to_string()]),
working_dir: None,
env: None,
hide_console: None,
run_as_admin: None,
signal_handling: None,
};
let executor = Executor::new(config);
assert_eq!(executor.config.path, "/bin/echo");
}
#[test]
fn test_executor_with_env() {
let mut env = HashMap::new();
env.insert("TEST_VAR".to_string(), "test_value".to_string());
let config = ShimConfig {
path: "/bin/echo".to_string(),
args: None,
working_dir: None,
env: Some(env),
hide_console: None,
run_as_admin: None,
signal_handling: None,
};
let executor = Executor::new(config);
let resolved_env = executor.config.resolved_env();
assert_eq!(
resolved_env.get("TEST_VAR"),
Some(&"test_value".to_string())
);
}
}