mirror of
https://codeberg.org/Cyborus/forgejo-cli.git
synced 2024-09-20 08:05:04 +02:00
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:
commit
f8650ab284
4 changed files with 19 additions and 22 deletions
19
src/auth.rs
19
src/auth.rs
|
@ -13,8 +13,6 @@ pub enum AuthCommand {
|
||||||
///
|
///
|
||||||
/// Use this if `fj auth login` doesn't work
|
/// Use this if `fj auth login` doesn't work
|
||||||
AddKey {
|
AddKey {
|
||||||
/// The domain name of the forgejo instance.
|
|
||||||
host: String,
|
|
||||||
/// The user that the key is associated with
|
/// The user that the key is associated with
|
||||||
user: String,
|
user: String,
|
||||||
/// The key to add. If not present, the key will be read in from stdin.
|
/// 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}");
|
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 {
|
let key = match key {
|
||||||
Some(key) => key,
|
Some(key) => key,
|
||||||
None => crate::readline("new key: ").await?.trim().to_string(),
|
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(
|
keys.hosts.insert(
|
||||||
host,
|
host.to_owned(),
|
||||||
crate::keys::LoginInfo::Application {
|
crate::keys::LoginInfo::Application {
|
||||||
name: user,
|
name: user,
|
||||||
token: key,
|
token: key,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
println!("key for {} already exists", host);
|
println!("key for {host} already exists");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AuthCommand::List => {
|
AuthCommand::List => {
|
||||||
|
@ -84,7 +85,7 @@ impl AuthCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_client_info_for(url: &url::Url) -> Option<(&'static str, &'static str)> {
|
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"),
|
("codeberg.org", "/") => option_env!("CLIENT_INFO_CODEBERG"),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
@ -173,8 +174,8 @@ async fn oauth_login(
|
||||||
refresh_token: response.refresh_token,
|
refresh_token: response.refresh_token,
|
||||||
expires_at,
|
expires_at,
|
||||||
};
|
};
|
||||||
keys.hosts
|
let domain = crate::host_with_port(&host);
|
||||||
.insert(host.host_str().unwrap().to_string(), login_info);
|
keys.hosts.insert(domain.to_owned(), login_info);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
14
src/keys.rs
14
src/keys.rs
|
@ -43,19 +43,11 @@ impl KeyInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_login(&mut self, url: &Url) -> eyre::Result<&mut LoginInfo> {
|
pub fn get_login(&mut self, url: &Url) -> eyre::Result<&mut LoginInfo> {
|
||||||
let host_str = url
|
let host = crate::host_with_port(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 login_info = self
|
let login_info = self
|
||||||
.hosts
|
.hosts
|
||||||
.get_mut(&domain)
|
.get_mut(host)
|
||||||
.ok_or_else(|| eyre!("not signed in to {domain}"))?;
|
.ok_or_else(|| eyre!("not signed in to {host}"))?;
|
||||||
Ok(login_info)
|
Ok(login_info)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
use std::sync::OnceLock;
|
||||||
static SPECIAL_RENDER: OnceLock<SpecialRender> = OnceLock::new();
|
static SPECIAL_RENDER: OnceLock<SpecialRender> = OnceLock::new();
|
||||||
|
|
||||||
|
|
|
@ -99,7 +99,7 @@ impl RepoInfo {
|
||||||
let url_s = std::str::from_utf8(remote.url_bytes())?;
|
let url_s = std::str::from_utf8(remote.url_bytes())?;
|
||||||
let url = crate::ssh_url_parse(url_s)?;
|
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());
|
name = Some(remote_name_s.to_owned());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -124,7 +124,7 @@ impl RepoInfo {
|
||||||
|
|
||||||
if let Some(url) = remote.url() {
|
if let Some(url) = remote.url() {
|
||||||
let (url, _) = url_strip_repo_name(crate::ssh_url_parse(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()
|
&& url.path() == host_url.path()
|
||||||
{
|
{
|
||||||
name = Some(remote_name.to_owned());
|
name = Some(remote_name.to_owned());
|
||||||
|
|
Loading…
Reference in a new issue