use crate::cmd::common::{create_temp_mountpoint, setup_signal_handler, unmount_internal};
use agentignore::fs::AgentFS;
use std::path::PathBuf;
use std::process::Command as ProcessCommand;
pub fn run(command: Vec<String>, source: Option<PathBuf>, show_config_files: bool) {
crate::cmd::doctor::check_prerequisites(true);
let source = source.unwrap_or_else(|| std::env::current_dir().unwrap());
let source = source.canonicalize().expect("source path must exist");
let mountpoint = create_temp_mountpoint(&source);
setup_signal_handler(Some(mountpoint.clone()), true);
println!("Mounting {:?} → {:?}", source, mountpoint);
let fs = AgentFS::with_config(source.clone(), None, show_config_files);
let mp_clone = mountpoint.clone();
let mount_handle = std::thread::spawn(move || {
fuser::mount2(fs, &mp_clone, &fuser::Config::default()).expect("mount failed");
});
std::thread::sleep(std::time::Duration::from_millis(100));
let program = &command[0];
let args = &command[1..];
let command_string = command.join(" ");
let result = ProcessCommand::new(program)
.args(args)
.current_dir(&mountpoint)
.env("PWD", &mountpoint)
.status();
unmount_internal(&mountpoint);
if let Err(e) = std::fs::remove_dir(&mountpoint) {
eprintln!(
"Warning: failed to remove temporary directory {:?}: {}",
mountpoint, e
);
}
let _ = mount_handle.join();
match result {
Ok(status) => {
std::process::exit(status.code().unwrap_or(1));
}
Err(e) => {
eprintln!("Failed to execute command '{}': {}", command_string, e);
std::process::exit(1);
}
}
}