use std::path::PathBuf;
use std::time::Duration;
use crate::hostkey::{
DEFAULT_CODEBERG_HOST, DEFAULT_GITHUB_HOST, DEFAULT_GITLAB_HOST, DEFAULT_PORT,
FALLBACK_PORT, GITHUB_FALLBACK_HOST, GITLAB_FALLBACK_HOST,
};
#[derive(Debug, Clone)]
pub struct GitwayConfig {
pub host: String,
pub port: u16,
pub username: String,
pub identity_file: Option<PathBuf>,
pub cert_file: Option<PathBuf>,
pub skip_host_check: bool,
pub inactivity_timeout: Duration,
pub custom_known_hosts: Option<PathBuf>,
pub verbose: bool,
pub fallback: Option<(String, u16)>,
}
impl GitwayConfig {
pub fn builder(host: impl Into<String>) -> GitwayConfigBuilder {
GitwayConfigBuilder::new(host.into())
}
#[must_use]
pub fn github() -> Self {
Self::builder(DEFAULT_GITHUB_HOST)
.fallback(Some((GITHUB_FALLBACK_HOST.to_owned(), FALLBACK_PORT)))
.build()
}
#[must_use]
pub fn gitlab() -> Self {
Self::builder(DEFAULT_GITLAB_HOST)
.fallback(Some((GITLAB_FALLBACK_HOST.to_owned(), FALLBACK_PORT)))
.build()
}
#[must_use]
pub fn codeberg() -> Self {
Self::builder(DEFAULT_CODEBERG_HOST).build()
}
}
#[derive(Debug)]
#[must_use]
pub struct GitwayConfigBuilder {
host: String,
port: u16,
username: String,
identity_file: Option<PathBuf>,
cert_file: Option<PathBuf>,
skip_host_check: bool,
inactivity_timeout: Duration,
custom_known_hosts: Option<PathBuf>,
verbose: bool,
fallback: Option<(String, u16)>,
}
impl GitwayConfigBuilder {
fn new(host: String) -> Self {
Self {
host,
port: DEFAULT_PORT,
username: "git".to_owned(),
identity_file: None,
cert_file: None,
skip_host_check: false,
inactivity_timeout: Duration::from_secs(60),
custom_known_hosts: None,
verbose: false,
fallback: None,
}
}
pub fn port(mut self, port: u16) -> Self {
self.port = port;
self
}
pub fn username(mut self, username: impl Into<String>) -> Self {
self.username = username.into();
self
}
pub fn identity_file(mut self, path: impl Into<PathBuf>) -> Self {
self.identity_file = Some(path.into());
self
}
pub fn cert_file(mut self, path: impl Into<PathBuf>) -> Self {
self.cert_file = Some(path.into());
self
}
pub fn skip_host_check(mut self, skip: bool) -> Self {
self.skip_host_check = skip;
self
}
pub fn inactivity_timeout(mut self, timeout: Duration) -> Self {
self.inactivity_timeout = timeout;
self
}
pub fn custom_known_hosts(mut self, path: impl Into<PathBuf>) -> Self {
self.custom_known_hosts = Some(path.into());
self
}
pub fn verbose(mut self, verbose: bool) -> Self {
self.verbose = verbose;
self
}
pub fn fallback(mut self, fallback: Option<(String, u16)>) -> Self {
self.fallback = fallback;
self
}
#[must_use]
pub fn build(self) -> GitwayConfig {
GitwayConfig {
host: self.host,
port: self.port,
username: self.username,
identity_file: self.identity_file,
cert_file: self.cert_file,
skip_host_check: self.skip_host_check,
inactivity_timeout: self.inactivity_timeout,
custom_known_hosts: self.custom_known_hosts,
verbose: self.verbose,
fallback: self.fallback,
}
}
}