From 5414ceec070dd7f0af3030124b6043fe01b7863b Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Wed, 13 Nov 2024 12:04:49 +0100 Subject: [PATCH] Make home directory optional. --- src/commands/key/generate.rs | 27 +++++++++++++++++---------- src/commands/key/list.rs | 5 ++++- src/main.rs | 4 ++-- src/sq.rs | 8 +++++--- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/commands/key/generate.rs b/src/commands/key/generate.rs index 26a4101c..ec9505a5 100644 --- a/src/commands/key/generate.rs +++ b/src/commands/key/generate.rs @@ -153,17 +153,24 @@ pub fn generate( rev_cert } else if on_keystore { - let dir = sq.home.data_dir(sequoia_directories::Component::Other( - "revocation-certificates".into())); - std::fs::create_dir_all(&dir) - .with_context(|| { - format!("While creating {}", dir.display()) - })?; + if let Some(home) = &sq.home { + let dir = home.data_dir(sequoia_directories::Component::Other( + "revocation-certificates".into())); + std::fs::create_dir_all(&dir) + .with_context(|| { + format!("While creating {}", dir.display()) + })?; - (cert, rev) = gen()?; - FileOrStdout::new( - Some(dir.join(format!("{}-revocation.pgp", - cert.fingerprint())))) + (cert, rev) = gen()?; + FileOrStdout::new( + Some(dir.join(format!("{}-revocation.pgp", + cert.fingerprint())))) + } else { + return Err(anyhow::anyhow!( + "Missing arguments: --rev-cert is mandatory if --home=none is \ + given." + )); + } } else { return Err(anyhow::anyhow!( "Missing arguments: --rev-cert is mandatory if --output is \ diff --git a/src/commands/key/list.rs b/src/commands/key/list.rs index ba0211cf..90ecb113 100644 --- a/src/commands/key/list.rs +++ b/src/commands/key/list.rs @@ -447,7 +447,10 @@ pub fn list(sq: Sq, mut cmd: cli::key::list::Command) -> Result<()> { let mut hint = sq.hint(format_args!( "There are no secret keys.")); - if sq.key_store_path.is_some() || ! sq.home.is_default_location() { + if sq.key_store_path.is_some() + || ! sq.home.as_ref() + .map(|h| h.is_default_location()).unwrap_or(false) + { hint = hint.hint(format_args!( "The non-default key store location {} is selected \ using the `{}` option. Consider using the default \ diff --git a/src/main.rs b/src/main.rs index b297ba63..0269dea2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -301,14 +301,14 @@ fn real_main() -> Result<()> { time_is_now, policy_as_of, policy: &policy, - home: if let Some(p) = c.home.as_ref().and_then(|a| a.path()) { + home: Some(if let Some(p) = c.home.as_ref().and_then(|a| a.path()) { sequoia_directories::Home::new(p)? } else { sequoia_directories::Home::default() .ok_or(anyhow::anyhow!("no default SEQUOIA_HOME \ on this platform"))? .clone() - }, + }), no_rw_cert_store: c.no_cert_store, cert_store_path: c.cert_store.as_ref().and_then(|a| a.path()), keyrings: c.keyring.clone(), diff --git a/src/sq.rs b/src/sq.rs index 1cda22f2..650e2be5 100644 --- a/src/sq.rs +++ b/src/sq.rs @@ -104,7 +104,7 @@ pub struct Sq<'store, 'rstore> pub time_is_now: bool, pub policy: &'rstore P<'rstore>, pub policy_as_of: SystemTime, - pub home: sequoia_directories::Home, + pub home: Option, // --no-cert-store #[deprecated] pub no_rw_cert_store: bool, @@ -161,7 +161,8 @@ impl<'store: 'rstore, 'rstore> Sq<'store, 'rstore> { } else if let Ok(path) = std::env::var("PGP_CERT_D") { Some(PathBuf::from(path)) } else { - Some(self.home.data_dir(sequoia_directories::Component::CertD)) + self.home.as_ref() + .map(|h| h.data_dir(sequoia_directories::Component::CertD)) } } @@ -366,7 +367,8 @@ impl<'store: 'rstore, 'rstore> Sq<'store, 'rstore> { } else if let Some(dir) = self.key_store_path.as_ref() { Ok(Some(dir.clone())) } else { - Ok(Some(self.home.data_dir(sequoia_directories::Component::Keystore))) + Ok(self.home.as_ref() + .map(|h| h.data_dir(sequoia_directories::Component::Keystore))) } }