use anyhow::Result;
use crate::remote;
pub fn run(name: Option<String>, credential: Option<String>, dir: Option<String>, command: Vec<String>) -> Result<()> {
let host = remote::resolve_host(name, credential)?;
let command = command.join(" ");
let dir = dir.or_else(|| host.dir.clone());
match &dir {
Some(dir) => eprintln!("[{}] {dir}$ {command}", host.label()),
None => eprintln!("[{}] {command}", host.label()),
}
let session = remote::connect(&host.server, &host.credential)?;
let remote_command = match &dir {
Some(dir) => {
let dialect = remote::dialect(&session, &host.server);
remote::check_quotable(dialect, &[dir])?;
dialect.run_in(dir, &command)
}
None => command.clone(),
};
let code = session.run(&remote_command)?;
crate::journal::record("exec", Some(&host.server.name), None, Some(&command));
std::process::exit(code as i32);
}