From 1e9adafae180028638bc74fbcf026bb0eea8992a Mon Sep 17 00:00:00 2001 From: FliegendeWurst <2012gdwu+github@posteo.de> Date: Fri, 7 May 2021 15:49:34 +0200 Subject: [PATCH] Display keyring errors --- src/main.rs | 20 ++++++++++++-------- src/util.rs | 1 + 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3c1eacd..960c54f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -212,6 +212,8 @@ fn ask_user_pass(opt: &Opt) -> Result<(String, String)> { }; #[cfg(feature = "keyring-auth")] let (pass, should_store); + #[cfg(feature = "keyring-auth")] + let keyring = Lazy::new(|| keyring::Keyring::new(env!("CARGO_PKG_NAME"), &user)); #[cfg(not(feature = "keyring-auth"))] let pass; cfg_if::cfg_if! { // TODO: deduplicate the logic below @@ -220,13 +222,16 @@ fn ask_user_pass(opt: &Opt) -> Result<(String, String)> { pass = password.clone(); should_store = true; } else if opt.keyring { - let keyring = keyring::Keyring::new(env!("CARGO_PKG_NAME"), &user); - if let Ok(password) = keyring.get_password() { - pass = password; - should_store = false; - } else { - pass = rpassword::read_password_from_tty(Some("Password: ")).context("password prompt")?; - should_store = true; + match keyring.get_password() { + Ok(password) => { + pass = password; + should_store = false; + }, + Err(e) => { + error!(e); + pass = rpassword::read_password_from_tty(Some("Password: ")).context("password prompt")?; + should_store = true; + } } } else { pass = rpassword::read_password_from_tty(Some("Password: ")).context("password prompt")?; @@ -242,7 +247,6 @@ fn ask_user_pass(opt: &Opt) -> Result<(String, String)> { }; #[cfg(feature = "keyring-auth")] if should_store && opt.keyring { - let keyring = keyring::Keyring::new(env!("CARGO_PKG_NAME"), &user); keyring.set_password(&pass).map_err(|x| anyhow!(x.to_string()))?; } Ok((user, pass)) diff --git a/src/util.rs b/src/util.rs index e0ef627..7404f13 100644 --- a/src/util.rs +++ b/src/util.rs @@ -8,6 +8,7 @@ use std::path::Path; use crate::Result; +/// Write all data to the specified path. Will overwrite previous file data. pub async fn write_file_data(path: impl AsRef, data: &mut R) -> Result<()> where R: AsyncRead + Unpin { let file = AsyncFile::create(path.as_ref()).await.context("failed to create file")?;