diff --git a/src/client.rs b/src/client.rs index a3d5d51..552aed1 100644 --- a/src/client.rs +++ b/src/client.rs @@ -156,7 +156,7 @@ impl DoshClientBuilder { .unwrap_or_default(); let raw_server = host_config.ssh.clone().unwrap_or_else(|| self.host.clone()); let ssh_port = host_config.ssh_port.or(self.client.config.ssh_port); - let ssh_config = load_sdk_ssh_config(host_config.ssh_config.as_deref(), ssh_port)?; + let ssh_config = load_sdk_ssh_config_for_target(&host_config, &raw_server, ssh_port)?; let udp_host = selected_sdk_udp_host( self.udp_host.as_deref(), &host_config, @@ -493,6 +493,10 @@ fn load_sdk_ssh_config(alias: Option<&str>, ssh_port: Option) -> Result, ssh_port: Option) -> Result, +) -> Result { + let (alias, explicit) = sdk_ssh_config_lookup(host_config, raw_server); + match load_sdk_ssh_config(Some(alias), ssh_port) { + Ok(config) => Ok(config), + Err(_) if !explicit => Ok(SdkSshConfig::default()), + Err(err) => Err(err), + } +} + +fn sdk_ssh_config_lookup<'a>(host_config: &'a HostConfig, raw_server: &'a str) -> (&'a str, bool) { + match host_config.ssh_config.as_deref().map(str::trim) { + Some(alias) if !alias.is_empty() => (alias, true), + _ => (raw_server, false), + } +} + fn parse_sdk_ssh_config(raw: &str) -> SdkSshConfig { let mut config = SdkSshConfig::default(); for line in raw.lines() { @@ -1003,6 +1027,26 @@ mod tests { ); } + #[test] + fn sdk_ssh_config_lookup_uses_raw_target_by_default() { + let host = HostConfig::default(); + + assert_eq!( + sdk_ssh_config_lookup(&host, "deploy@prod"), + ("deploy@prod", false) + ); + } + + #[test] + fn sdk_ssh_config_lookup_prefers_explicit_alias() { + let host = HostConfig { + ssh_config: Some(" prod ".to_string()), + ..HostConfig::default() + }; + + assert_eq!(sdk_ssh_config_lookup(&host, "deploy@raw"), ("prod", true)); + } + #[test] fn sdk_identity_paths_follow_cli_precedence_and_identities_only() { let dir = tempfile::tempdir().unwrap();