Display keyring errors

This commit is contained in:
FliegendeWurst 2021-05-07 15:49:34 +02:00
parent bc8bc9ed0d
commit 1e9adafae1
2 changed files with 13 additions and 8 deletions

View File

@ -212,6 +212,8 @@ fn ask_user_pass(opt: &Opt) -> Result<(String, String)> {
}; };
#[cfg(feature = "keyring-auth")] #[cfg(feature = "keyring-auth")]
let (pass, should_store); 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"))] #[cfg(not(feature = "keyring-auth"))]
let pass; let pass;
cfg_if::cfg_if! { // TODO: deduplicate the logic below 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(); pass = password.clone();
should_store = true; should_store = true;
} else if opt.keyring { } else if opt.keyring {
let keyring = keyring::Keyring::new(env!("CARGO_PKG_NAME"), &user); match keyring.get_password() {
if let Ok(password) = keyring.get_password() { Ok(password) => {
pass = password; pass = password;
should_store = false; should_store = false;
} else { },
pass = rpassword::read_password_from_tty(Some("Password: ")).context("password prompt")?; Err(e) => {
should_store = true; error!(e);
pass = rpassword::read_password_from_tty(Some("Password: ")).context("password prompt")?;
should_store = true;
}
} }
} else { } else {
pass = rpassword::read_password_from_tty(Some("Password: ")).context("password prompt")?; 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")] #[cfg(feature = "keyring-auth")]
if should_store && opt.keyring { 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()))?; keyring.set_password(&pass).map_err(|x| anyhow!(x.to_string()))?;
} }
Ok((user, pass)) Ok((user, pass))

View File

@ -8,6 +8,7 @@ use std::path::Path;
use crate::Result; use crate::Result;
/// Write all data to the specified path. Will overwrite previous file data.
pub async fn write_file_data<R: ?Sized>(path: impl AsRef<Path>, data: &mut R) -> Result<()> pub async fn write_file_data<R: ?Sized>(path: impl AsRef<Path>, data: &mut R) -> Result<()>
where R: AsyncRead + Unpin { where R: AsyncRead + Unpin {
let file = AsyncFile::create(path.as_ref()).await.context("failed to create file")?; let file = AsyncFile::create(path.as_ref()).await.context("failed to create file")?;