diff --git a/src/commands.rs b/src/commands.rs index 4c1ca7f..ebe6038 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -21,6 +21,8 @@ pub enum CommandError { CertNotAlive(Fingerprint), #[error("Certificate is revoked: {0}")] CertRevoked(Fingerprint), + #[error("No signing key available")] + SigningKeyNotAvailable, } pub async fn dispatch(cli: CliArgs, config: HuskConfigContainer) -> Result<()> { diff --git a/src/commands/signkey.rs b/src/commands/signkey.rs index 97a235a..6a6e959 100644 --- a/src/commands/signkey.rs +++ b/src/commands/signkey.rs @@ -7,11 +7,13 @@ use sequoia_openpgp::serialize::SerializeInto; use crate::cli::cli_args::{SignkeyCommand, SignkeySubcommand}; use crate::config::HuskConfigContainer; use crate::types::husk_context::HuskContext; +use crate::commands::CommandError; pub mod set; pub mod create; -pub async fn dispatch(cmd: SignkeyCommand, config: HuskConfigContainer) -> Result<()> { +pub async fn dispatch(cmd: SignkeyCommand, config: HuskConfigContainer) + -> Result<()> { match cmd.subcommand { SignkeySubcommand::Set(set_cmd) => { set::dispatch(set_cmd, config).await?; @@ -24,25 +26,30 @@ pub async fn dispatch(cmd: SignkeyCommand, config: HuskConfigContainer) -> Resul if let Some(key) = context.signing_key { println!("Fingerprint: {}", key.fingerprint()); } else { - eprintln!("No active signing key available."); + return Err(CommandError::SigningKeyNotAvailable.into()); } }, SignkeySubcommand::Dump => { let context = HuskContext::new(&config.into())?; if let Some(key) = context.signing_key { - if let Ok(cert) = &context.cert_store.lookup_by_cert_fpr(&key.fingerprint()) { + if let Ok(cert) = &context.cert_store + .lookup_by_cert_fpr(&key.fingerprint()) { if let Ok(cert) = cert.to_cert() { let armored = cert.armored().to_vec()?; println!("{}", String::from_utf8_lossy(&armored[..])); } else { - eprintln!("Certificate {} is damaged", cert.fingerprint()); + return Err( + CommandError::CertNotUsable(cert.fingerprint()) + .into()); } } else { - eprintln!("Cannot find certificate for key {}", key.fingerprint()); + return Err( + CommandError::CertNotFound(key.fingerprint()) + .into()); } } else { - eprintln!("No active signing key available."); + return Err(CommandError::SigningKeyNotAvailable.into()); } } } diff --git a/src/commands/signkey/set.rs b/src/commands/signkey/set.rs index f79f36b..8c276a0 100644 --- a/src/commands/signkey/set.rs +++ b/src/commands/signkey/set.rs @@ -14,7 +14,8 @@ use crate::common::crypto; use crate::config::HuskConfigContainer; use crate::types::husk_context::HuskContext; -pub async fn dispatch(cmd: SignkeySetCommand, config: HuskConfigContainer) -> Result<()> { +pub async fn dispatch(cmd: SignkeySetCommand, config: HuskConfigContainer) + -> Result<()> { let context = HuskContext::new(&config.into())?; let policy = &context.policy; let cert_store = &context.cert_store; @@ -42,7 +43,8 @@ pub async fn dispatch(cmd: SignkeySetCommand, config: HuskConfigContainer) -> Re } else if let Some(cert) = cmd.cert { let fpr = Fingerprint::from_hex(cert.as_str())?; - let certs = cert_store.lookup_by_cert(&KeyHandle::from(&fpr)) + let certs = cert_store + .lookup_by_cert(&KeyHandle::from(&fpr)) .map_err(|_| CommandError::CertNotFound(fpr.clone()))?; let certs: Vec = certs.iter() diff --git a/src/common/crypto.rs b/src/common/crypto.rs index 7dd56d4..acfe245 100644 --- a/src/common/crypto.rs +++ b/src/common/crypto.rs @@ -703,10 +703,9 @@ pub fn import_key(sequoia_home: Home, cert: &Cert) -> bool { if let Ok(mut backends) = keystore.backends() { for backend in &mut backends { if let Ok(id) = backend.id() { - if id == "softkeys" { - if let Ok(_) = backend.import(cert) { - imported = true; - } + if id == "softkeys" + && backend.import(cert).is_ok() { + imported = true; } } }