Merge pull request 'improve host name consistency' (#124) from host-consistency into main

Reviewed-on: https://codeberg.org/Cyborus/forgejo-cli/pulls/124
This commit is contained in:
Cyborus 2024-09-05 16:11:30 +00:00
commit f8650ab284
4 changed files with 19 additions and 22 deletions

View file

@ -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(())
}

View file

@ -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)
}

View file

@ -223,6 +223,10 @@ fn ssh_url_parse(s: &str) -> Result<url::Url, url::ParseError> {
})
}
fn host_with_port(url: &url::Url) -> &str {
&url[url::Position::BeforeHost..url::Position::AfterPort]
}
use std::sync::OnceLock;
static SPECIAL_RENDER: OnceLock<SpecialRender> = OnceLock::new();

View file

@ -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());