use std::path::PathBuf;
#[derive(Clone, Debug)]
pub enum SshAuth {
Password(String),
KeyFile {
path: PathBuf,
passphrase: Option<String>,
},
None,
}
#[derive(Clone, Debug)]
pub enum SshHostKeyPolicy {
AcceptAny,
KnownHosts,
KnownHostsFile(PathBuf),
}
#[derive(Clone, Debug)]
pub struct SshConfig {
username: Option<String>,
pub(super) auth: SshAuth,
pub(super) host_key: SshHostKeyPolicy,
pub(super) command: String,
pub(super) use_openssh_config: bool,
pub(super) try_ssh_agent: bool,
pub(super) try_default_identities: bool,
pub(super) accept_new_host_keys: bool,
}
impl Default for SshConfig {
fn default() -> Self {
Self {
username: None,
auth: SshAuth::None,
host_key: SshHostKeyPolicy::KnownHosts,
command: "svnserve -t".to_string(),
use_openssh_config: true,
try_ssh_agent: true,
try_default_identities: true,
accept_new_host_keys: false,
}
}
}
impl SshConfig {
pub fn new(auth: SshAuth) -> Self {
Self {
username: None,
auth,
host_key: SshHostKeyPolicy::KnownHosts,
command: "svnserve -t".to_string(),
use_openssh_config: true,
try_ssh_agent: false,
try_default_identities: false,
accept_new_host_keys: false,
}
}
#[must_use]
pub fn with_username(mut self, username: impl Into<String>) -> Self {
self.username = Some(username.into());
self
}
#[must_use]
pub fn accept_any_host_key(mut self) -> Self {
self.host_key = SshHostKeyPolicy::AcceptAny;
self
}
#[must_use]
pub fn with_known_hosts_file(mut self, path: impl Into<PathBuf>) -> Self {
self.host_key = SshHostKeyPolicy::KnownHostsFile(path.into());
self
}
#[must_use]
pub fn with_command(mut self, command: impl Into<String>) -> Self {
self.command = command.into();
self
}
#[must_use]
pub fn with_openssh_config(mut self, enabled: bool) -> Self {
self.use_openssh_config = enabled;
self
}
#[must_use]
pub fn with_ssh_agent(mut self) -> Self {
self.try_ssh_agent = true;
self
}
#[must_use]
pub fn with_default_identities(mut self) -> Self {
self.try_default_identities = true;
self
}
#[must_use]
pub fn accept_new_host_keys(mut self) -> Self {
self.accept_new_host_keys = true;
self
}
pub(super) fn username_override(&self) -> Option<&str> {
self.username.as_deref()
}
}