summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCyborus <cyborus@noreply.codeberg.org>2024-09-05 18:11:30 +0200
committerCyborus <cyborus@noreply.codeberg.org>2024-09-05 18:11:30 +0200
commitf8650ab284ce549fdd52e52e753fe29e753efe80 (patch)
treedaa9fd486441b869969df92880117659b3b2730b
parentMerge pull request 'don't accept cannot-be-a-base urls in parsing' (#123) fro... (diff)
parentfix: consistency among host names (diff)
downloadforgejo-cli-f8650ab284ce549fdd52e52e753fe29e753efe80.tar.xz
forgejo-cli-f8650ab284ce549fdd52e52e753fe29e753efe80.zip
Merge pull request 'improve host name consistency' (#124) from host-consistency into main
Reviewed-on: https://codeberg.org/Cyborus/forgejo-cli/pulls/124
-rw-r--r--src/auth.rs19
-rw-r--r--src/keys.rs14
-rw-r--r--src/main.rs4
-rw-r--r--src/repo.rs4
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<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();
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());