From 14e0b7d4ef1bbae9290e86f14c58a08b746270f2 Mon Sep 17 00:00:00 2001 From: Cyborus Date: Thu, 5 Sep 2024 12:01:03 -0400 Subject: [PATCH] fix: consistency among host names --- src/auth.rs | 19 ++++++++++--------- src/keys.rs | 14 +++----------- src/main.rs | 4 ++++ src/repo.rs | 4 ++-- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index c0bd467..40dedd5 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -13,8 +13,6 @@ pub enum AuthCommand { /// /// Use this if `fj auth login` doesn't work AddKey { - /// The domain name of the forgejo instance. - host: String, /// The user that the key is associated with user: String, /// The key to add. If not present, the key will be read in from stdin. @@ -53,21 +51,24 @@ impl AuthCommand { eprintln!("already not signed in to {host}"); } } - AuthCommand::AddKey { host, user, key } => { + AuthCommand::AddKey { user, key } => { + let repo_info = crate::repo::RepoInfo::get_current(host_name, None, None)?; + let host_url = repo_info.host_url(); let key = match key { Some(key) => key, None => crate::readline("new key: ").await?.trim().to_string(), }; - if !keys.hosts.contains_key(&user) { + let host = crate::host_with_port(&host_url); + if !keys.hosts.contains_key(host) { keys.hosts.insert( - host, + host.to_owned(), crate::keys::LoginInfo::Application { name: user, token: key, }, ); } else { - println!("key for {} already exists", host); + println!("key for {host} already exists"); } } AuthCommand::List => { @@ -84,7 +85,7 @@ impl AuthCommand { } pub fn get_client_info_for(url: &url::Url) -> Option<(&'static str, &'static str)> { - let client_info = match (url.host_str()?, url.path()) { + let client_info = match (crate::host_with_port(url), url.path()) { ("codeberg.org", "/") => option_env!("CLIENT_INFO_CODEBERG"), _ => None, }; @@ -173,8 +174,8 @@ async fn oauth_login( refresh_token: response.refresh_token, expires_at, }; - keys.hosts - .insert(host.host_str().unwrap().to_string(), login_info); + let domain = crate::host_with_port(&host); + keys.hosts.insert(domain.to_owned(), login_info); Ok(()) } diff --git a/src/keys.rs b/src/keys.rs index 33ac02e..58fc57d 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -43,19 +43,11 @@ impl KeyInfo { } pub fn get_login(&mut self, url: &Url) -> eyre::Result<&mut LoginInfo> { - let host_str = url - .host_str() - .ok_or_else(|| eyre!("remote url does not have host"))?; - let domain = if let Some(port) = url.port() { - format!("{}:{}", host_str, port) - } else { - host_str.to_owned() - }; - + let host = crate::host_with_port(url); let login_info = self .hosts - .get_mut(&domain) - .ok_or_else(|| eyre!("not signed in to {domain}"))?; + .get_mut(host) + .ok_or_else(|| eyre!("not signed in to {host}"))?; Ok(login_info) } diff --git a/src/main.rs b/src/main.rs index f224abd..881bc40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -223,6 +223,10 @@ fn ssh_url_parse(s: &str) -> Result { }) } +fn host_with_port(url: &url::Url) -> &str { + &url[url::Position::BeforeHost..url::Position::AfterPort] +} + use std::sync::OnceLock; static SPECIAL_RENDER: OnceLock = OnceLock::new(); diff --git a/src/repo.rs b/src/repo.rs index c8aa924..5221bf3 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -99,7 +99,7 @@ impl RepoInfo { let url_s = std::str::from_utf8(remote.url_bytes())?; let url = crate::ssh_url_parse(url_s)?; - if url.host_str() == host_url.host_str() { + if crate::host_with_port(&url) == crate::host_with_port(host_url) { name = Some(remote_name_s.to_owned()); } } else { @@ -124,7 +124,7 @@ impl RepoInfo { if let Some(url) = remote.url() { let (url, _) = url_strip_repo_name(crate::ssh_url_parse(url)?)?; - if url.host_str() == host_url.host_str() + if crate::host_with_port(&url) == crate::host_with_port(host_url) && url.path() == host_url.path() { name = Some(remote_name.to_owned());