feat: automatically add ssh url as alias if it is different

This commit is contained in:
Cyborus 2024-09-05 14:17:32 -04:00
parent 7121e26215
commit 0e6ae3962a
No known key found for this signature in database

View file

@ -60,13 +60,12 @@ impl AuthCommand {
}; };
let host = crate::host_with_port(&host_url); let host = crate::host_with_port(&host_url);
if !keys.hosts.contains_key(host) { if !keys.hosts.contains_key(host) {
keys.hosts.insert( let mut login = crate::keys::LoginInfo::Application {
host.to_owned(),
crate::keys::LoginInfo::Application {
name: user, name: user,
token: key, token: key,
}, };
); add_ssh_alias(&mut login, host_url, keys).await;
keys.hosts.insert(host.to_owned(), login);
} else { } else {
println!("key for {host} already exists"); println!("key for {host} already exists");
} }
@ -168,12 +167,13 @@ async fn oauth_login(
// expires. Better to refresh slightly too soon than slightly too late. // expires. Better to refresh slightly too soon than slightly too late.
let expires_in = std::time::Duration::from_secs(response.expires_in.saturating_sub(60) as u64); let expires_in = std::time::Duration::from_secs(response.expires_in.saturating_sub(60) as u64);
let expires_at = time::OffsetDateTime::now_utc() + expires_in; let expires_at = time::OffsetDateTime::now_utc() + expires_in;
let login_info = crate::keys::LoginInfo::OAuth { let mut login_info = crate::keys::LoginInfo::OAuth {
name, name,
token: response.access_token, token: response.access_token,
refresh_token: response.refresh_token, refresh_token: response.refresh_token,
expires_at, expires_at,
}; };
add_ssh_alias(&mut login_info, host, keys).await;
let domain = crate::host_with_port(&host); let domain = crate::host_with_port(&host);
keys.hosts.insert(domain.to_owned(), login_info); keys.hosts.insert(domain.to_owned(), login_info);
@ -232,3 +232,38 @@ fn auth_server() -> (
}); });
(handle, rx) (handle, rx)
} }
async fn add_ssh_alias(
login: &mut crate::keys::LoginInfo,
host_url: &url::Url,
keys: &mut crate::keys::KeyInfo,
) {
let api = match login.api_for(host_url).await {
Ok(x) => x,
Err(_) => return,
};
if let Some(ssh_url) = get_instance_ssh_url(api).await {
let http_host = crate::host_with_port(&host_url);
let ssh_host = crate::host_with_port(&ssh_url);
if http_host != ssh_host {
keys.aliases
.insert(ssh_host.to_string(), http_host.to_string());
}
}
}
async fn get_instance_ssh_url(api: forgejo_api::Forgejo) -> Option<url::Url> {
let query = forgejo_api::structs::RepoSearchQuery {
limit: Some(1),
..Default::default()
};
let results = api.repo_search(query).await.ok()?;
if let Some(mut repos) = results.data {
if let Some(repo) = repos.pop() {
if let Some(ssh_url) = repo.ssh_url {
return Some(ssh_url);
}
}
}
None
}