use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct JumpHost {
pub user: Option<String>,
pub host: String,
pub port: Option<u16>,
pub ssh_key: Option<String>,
}
impl JumpHost {
pub fn new(host: String, user: Option<String>, port: Option<u16>) -> Self {
Self {
user,
host,
port,
ssh_key: None,
}
}
pub fn with_ssh_key(
host: String,
user: Option<String>,
port: Option<u16>,
ssh_key: Option<String>,
) -> Self {
Self {
user,
host,
port,
ssh_key,
}
}
pub fn effective_user(&self) -> String {
self.user
.clone()
.unwrap_or_else(|| whoami::username().unwrap_or_else(|_| "user".to_string()))
}
pub fn effective_port(&self) -> u16 {
self.port.unwrap_or(22)
}
pub fn to_connection_string(&self) -> String {
match (&self.user, &self.port) {
(Some(user), Some(port)) => format!("{}@{}:{}", user, self.host, port),
(Some(user), None) => format!("{}@{}", user, self.host),
(None, Some(port)) => format!("{}:{}", self.host, port),
(None, None) => self.host.clone(),
}
}
}
impl fmt::Display for JumpHost {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_connection_string())
}
}