Refactor HuskContext

- Provide a way to initialize a `HuskContext` without determine the
  signing key. This saves startup time for commands which do not need
  the signing key.
This commit is contained in:
Malte Meiboom
2026-08-06 11:47:32 +02:00
parent cd8935b3f3
commit 118b930c1d
+23 -10
View File
@@ -36,8 +36,17 @@ pub struct HuskContext<'hc> {
impl<'hc> HuskContext<'hc> {
/// Create a new HuskContext instance.
pub fn new(config: &HuskConfig) -> anyhow::Result<HuskContext<'hc>> {
Self::create(config, true)
}
/// Create a new HuskContext without determine the signing key
pub fn without_signkey(config: &HuskConfig) -> anyhow::Result<HuskContext<'hc>> {
Self::create(config, false)
}
/// Create a new HuskContext instance.
pub fn create(config: &HuskConfig, with_signkey: bool) -> anyhow::Result<HuskContext<'hc>> {
let sequoia_home = Home::new(PathBuf::from(&config.sequoia_home))?;
let cert_store_base = sequoia_home.data_dir(sequoia_directories::Component::CertD);
@@ -48,16 +57,20 @@ impl<'hc> HuskContext<'hc> {
let local_trust_root = crypto::get_local_trust_root(&cert_store)?;
let introducers = crypto::get_introducers(&cert_store, &policy, local_trust_root.fingerprint());
let signing_key = match crypto::get_signing_key(sequoia_home, &cert_store, &policy, &local_trust_root) {
Ok(Some(cert)) => Some(cert),
Ok(None) => {
log::warn!("No signing key found");
None
},
Err(e) => {
log::error!("Error while fetching signing key: {}", e);
None
let signing_key = if with_signkey {
match crypto::get_signing_key(sequoia_home, &cert_store, &policy, &local_trust_root) {
Ok(Some(cert)) => Some(cert),
Ok(None) => {
log::warn!("No signing key found");
None
},
Err(e) => {
log::error!("Error while fetching signing key: {}", e);
None
}
}
} else {
None
};
Ok(HuskContext {