Use error types, cleanup

- Create and use `CommandError` in signkey commands instead of
  `eprintln!()`.
- Minor cleanup in `crypto.rs`.
This commit is contained in:
Malte Meiboom
2026-06-24 11:19:07 +02:00
parent 7688e6c5a2
commit dabd3b26f9
4 changed files with 22 additions and 12 deletions
+2
View File
@@ -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<()> {
+13 -6
View File
@@ -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());
}
}
}
+4 -2
View File
@@ -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<ValidCert> = certs.iter()
+3 -4
View File
@@ -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;
}
}
}