diff options
-rw-r--r-- | src/keys.rs | 25 | ||||
-rw-r--r-- | src/main.rs | 2 |
2 files changed, 14 insertions, 13 deletions
diff --git a/src/keys.rs b/src/keys.rs index e5fd6c5..5239df6 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -1,4 +1,5 @@ use eyre::eyre; +use forgejo_api::{Auth, Forgejo}; use std::{collections::BTreeMap, io::ErrorKind}; use tokio::io::AsyncWriteExt; use url::Url; @@ -44,17 +45,17 @@ impl KeyInfo { Ok(()) } - pub fn get_login(&mut self, url: &Url) -> eyre::Result<&mut LoginInfo> { + pub fn get_login(&mut self, url: &Url) -> Option<&mut LoginInfo> { let host = crate::host_with_port(url); - let login_info = self - .hosts - .get_mut(host) - .ok_or_else(|| eyre!("not signed in to {host}"))?; - Ok(login_info) + let login_info = self.hosts.get_mut(host)?; + Some(login_info) } - pub async fn get_api(&mut self, url: &Url) -> eyre::Result<forgejo_api::Forgejo> { - self.get_login(url)?.api_for(url).await.map_err(Into::into) + pub async fn get_api(&mut self, url: &Url) -> eyre::Result<Forgejo> { + match self.get_login(url) { + Some(login) => login.api_for(url).await, + None => Forgejo::new(Auth::None, url.clone()).map_err(Into::into), + } } pub fn deref_alias(&self, url: url::Url) -> url::Url { @@ -96,10 +97,10 @@ impl LoginInfo { } } - pub async fn api_for(&mut self, url: &Url) -> eyre::Result<forgejo_api::Forgejo> { + pub async fn api_for(&mut self, url: &Url) -> eyre::Result<Forgejo> { match self { LoginInfo::Application { token, .. } => { - let api = forgejo_api::Forgejo::new(forgejo_api::Auth::Token(token), url.clone())?; + let api = Forgejo::new(Auth::Token(token), url.clone())?; Ok(api) } LoginInfo::OAuth { @@ -109,7 +110,7 @@ impl LoginInfo { .. } => { if time::OffsetDateTime::now_utc() >= *expires_at { - let api = forgejo_api::Forgejo::new(forgejo_api::Auth::None, url.clone())?; + let api = Forgejo::new(Auth::None, url.clone())?; let (client_id, client_secret) = crate::auth::get_client_info_for(url) .ok_or_else(|| { eyre::eyre!("Can't refresh token; no client info for {url}. How did this happen?") @@ -130,7 +131,7 @@ impl LoginInfo { ); *expires_at = time::OffsetDateTime::now_utc() + expires_in; } - let api = forgejo_api::Forgejo::new(forgejo_api::Auth::Token(token), url.clone())?; + let api = Forgejo::new(Auth::Token(token), url.clone())?; Ok(api) } } diff --git a/src/main.rs b/src/main.rs index 5aaf2d0..06a9965 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,7 +69,7 @@ async fn main() -> eyre::Result<()> { .wrap_err("could not find host, try specifying with --host")? .host_url() .clone(); - let name = keys.get_login(&url)?.username(); + let name = keys.get_login(&url).ok_or_eyre("not logged in")?.username(); let host = url .host_str() .ok_or_eyre("instance url does not have host")?; |