From 4f83bb9a3f1b195bba6a823c1559e035e41cb871 Mon Sep 17 00:00:00 2001 From: Tyler Beckman Date: Thu, 28 Sep 2023 13:13:57 -0600 Subject: [PATCH] Add success text to import gpg --- src/commands/keys/import/gpg.rs | 52 ++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/src/commands/keys/import/gpg.rs b/src/commands/keys/import/gpg.rs index 9103b22..b449371 100644 --- a/src/commands/keys/import/gpg.rs +++ b/src/commands/keys/import/gpg.rs @@ -9,6 +9,7 @@ use data_encoding::{BASE64URL_NOPAD, BASE64_NOPAD}; use dialoguer::{theme::ColorfulTheme, Password}; use elliptic_curve::sec1::{Coordinates, ToEncodedPoint}; use gpgme::{Context as GpgContext, PassphraseRequest}; +use indoc::printdoc; use pgp::{ crypto::ecc_curve::ECCCurve, types::{EcdsaPublicParams, KeyTrait, PlainSecretParams, PublicParams, SecretParams}, @@ -38,9 +39,7 @@ impl AspmSubcommand for KeysImportGpgCommand { .secret_keys() .context("Unable to fetch GPG secret keys")? { - let Ok(key) = key else { - continue - }; + let Ok(key) = key else { continue }; if key.fingerprint().unwrap_or("") != self.fingerprint && key @@ -54,9 +53,9 @@ impl AspmSubcommand for KeysImportGpgCommand { break; } let Some(found_key) = found_key else { - eprintln!("No key was found matching the provided fingerprint"); - std::process::exit(1); - }; + eprintln!("No key was found matching the provided fingerprint"); + std::process::exit(1); + }; // For some reason, when exporting secret keys, GPG will prompt for a password but then still // export the key in an encrypted form. In order to prevent a password needing to be entered @@ -104,8 +103,8 @@ impl AspmSubcommand for KeysImportGpgCommand { let parsed = parsed.next().context("Invalid GPG data")?; let Ok(key) = parsed else { - bail!("GPG data was unable to be parsed"); - }; + bail!("GPG data was unable to be parsed"); + }; let key = key.into_secret(); let Some(uid) = key @@ -113,16 +112,17 @@ impl AspmSubcommand for KeysImportGpgCommand { .users .iter() .find(|uid| uid.is_primary()) - .or_else(|| { - if key.details.users.len() == 1 { - Some(&key.details.users[0]) - } else { - None - } - }) else { - eprintln!("Key being imported has no primary uid. This must be set, as it is used for the key alias."); - std::process::exit(1); - }; + .or_else(|| { + if key.details.users.len() == 1 { + Some(&key.details.users[0]) + } else { + None + } + }) + else { + eprintln!("Key being imported has no primary uid. This must be set, as it is used for the key alias."); + std::process::exit(1); + }; let (algorithm, public_params, secret_params) = { if key .primary_key @@ -193,8 +193,8 @@ impl AspmSubcommand for KeysImportGpgCommand { let encoded = public_key.to_encoded_point(false); let Coordinates::Uncompressed { x, y } = encoded.coordinates() else { - bail!("EC key coordinates were not uncompressed") - }; + bail!("EC key coordinates were not uncompressed") + }; map.insert("x".to_string(), BASE64URL_NOPAD.encode(x.as_slice()).into()); map.insert("y".to_string(), BASE64URL_NOPAD.encode(y.as_slice()).into()); map @@ -255,7 +255,7 @@ impl AspmSubcommand for KeysImportGpgCommand { let entry = keys::ActiveModel { fingerprint: ActiveValue::Set(asp_key.fingerprint.clone()), - key_type: ActiveValue::Set(asp_key.key_type.into()), + key_type: ActiveValue::Set(asp_key.key_type.clone().into()), alias: ActiveValue::Set(format!("{uid}", uid = uid.id.id())), encrypted: ActiveValue::Set(encrypted), }; @@ -267,6 +267,16 @@ impl AspmSubcommand for KeysImportGpgCommand { bail!("The key was unable to be saved to the database") } + printdoc! { + " + Successfully imported key! + ASP Fingerprint: {fpr} + Type: {type:?} + ", + fpr = asp_key.fingerprint, + r#type = asp_key.key_type + }; + Ok(()) } }