use super::route::{self, Route};
use super::{Module, Ran};
use crate::Result;
use crate::cli::CliError;
use crate::console::ShellCommand;
use crate::error::Error;
use crate::graphical::no_console;
const SSH_FAILED: i32 = 255;
pub(super) async fn of(module: &Module<'_, '_>, machine: &str, user: Option<&str>) -> Result<ShellCommand> {
let route = route::to(module, machine, None).await?.map_err(|why| no_console(machine, why))?;
let mut args = route.options(module).await?;
args.extend(["-t".to_owned(), route.to.target()]);
args.extend(user.map(crate::console::shells::login_shell_as));
Ok(ShellCommand::new("ssh", args, module.cx.project.dir.clone()))
}
pub(super) async fn exec(module: &Module<'_, '_>, machine: &str, argv: &[String]) -> Result<Ran> {
let route: Route = route::to(module, machine, None).await?.map_err(|why| Error::Unsupported {
what: format!("running a command in `{machine}`: {why}"),
})?;
let mut ssh_args = route.options(module).await?;
let command = argv.iter().map(|w| crate::console::sh_word(w)).collect::<Vec<_>>().join(" ");
ssh_args.extend([route.to.target(), command]);
let call = module.cx.call(super::super::CHECK_LIMIT);
let out = call.run::<String>("ssh", ssh_args).await.map_err(|e| module.error("exec", e))?;
let exit = out.exit.code.unwrap_or(-1);
if exit == SSH_FAILED {
return Err(module.error(
"exec",
CliError::Failed {
tool: "ssh",
failure: format!("it couldn't run the command in `{machine}` ({}): {}", route.to.target(), out.reason()),
},
));
}
Ok(Ran {
exit,
stdout: out.stdout.join("\n"),
stderr: out.stderr_tail.join("\n"),
})
}