diff --git a/lib/src/gpg_signing.rs b/lib/src/gpg_signing.rs index 273dc2edd..2120ec569 100644 --- a/lib/src/gpg_signing.rs +++ b/lib/src/gpg_signing.rs @@ -25,6 +25,7 @@ use std::str; use thiserror::Error; +use crate::settings::ConfigResultExt as _; use crate::signing::SigStatus; use crate::signing::SignError; use crate::signing::SigningBackend; @@ -143,16 +144,16 @@ impl GpgBackend { self } - pub fn from_config(config: &config::Config) -> Self { - Self::new( - config - .get_string("signing.backends.gpg.program") - .unwrap_or_else(|_| "gpg".into()) - .into(), - config - .get_bool("signing.backends.gpg.allow-expired-keys") - .unwrap_or(false), - ) + pub fn from_config(config: &config::Config) -> Result { + let program = config + .get_string("signing.backends.gpg.program") + .optional()? + .unwrap_or_else(|| "gpg".into()); + let allow_expired_keys = config + .get_bool("signing.backends.gpg.allow-expired-keys") + .optional()? + .unwrap_or(false); + Ok(Self::new(program.into(), allow_expired_keys)) } fn create_command(&self) -> Command { diff --git a/lib/src/signing.rs b/lib/src/signing.rs index aa5b26ddc..16dfb079c 100644 --- a/lib/src/signing.rs +++ b/lib/src/signing.rs @@ -119,6 +119,9 @@ pub enum SignInitError { /// If the backend name specified in the config is not known. #[error("Unknown signing backend configured: {0}")] UnknownBackend(String), + /// Failed to load backend configuration. + #[error("Failed to configure signing backend")] + BackendConfig(#[source] config::ConfigError), } /// A enum that describes if a created/rewritten commit should be signed or not. @@ -158,10 +161,14 @@ impl Signer { /// Creates a signer based on user settings. Uses all known backends, and /// chooses one of them to be used for signing depending on the config. pub fn from_settings(settings: &UserSettings) -> Result { - let mut backends = vec![ - Box::new(GpgBackend::from_config(settings.config())) as Box, - Box::new(SshBackend::from_config(settings.config())) as Box, - // Box::new(X509Backend::from_settings(settings)?) as Box, + let mut backends: Vec> = vec![ + Box::new( + GpgBackend::from_config(settings.config()).map_err(SignInitError::BackendConfig)?, + ), + Box::new( + SshBackend::from_config(settings.config()).map_err(SignInitError::BackendConfig)?, + ), + // Box::new(X509Backend::from_settings(settings).map_err(..)?), ]; let main_backend = settings diff --git a/lib/src/ssh_signing.rs b/lib/src/ssh_signing.rs index 202e0193a..f858c3fb3 100644 --- a/lib/src/ssh_signing.rs +++ b/lib/src/ssh_signing.rs @@ -26,6 +26,7 @@ use std::process::Stdio; use either::Either; use thiserror::Error; +use crate::settings::ConfigResultExt as _; use crate::signing::SigStatus; use crate::signing::SignError; use crate::signing::SigningBackend; @@ -116,16 +117,15 @@ impl SshBackend { } } - pub fn from_config(config: &config::Config) -> Self { - Self::new( - config - .get_string("signing.backends.ssh.program") - .unwrap_or_else(|_| "ssh-keygen".into()) - .into(), - config - .get_string("signing.backends.ssh.allowed-signers") - .map_or(None, |v| Some(v.into())), - ) + pub fn from_config(config: &config::Config) -> Result { + let program = config + .get_string("signing.backends.ssh.program") + .optional()? + .unwrap_or_else(|| "ssh-keygen".into()); + let allowed_signers = config + .get_string("signing.backends.ssh.allowed-signers") + .optional()?; + Ok(Self::new(program.into(), allowed_signers.map(|v| v.into()))) } fn create_command(&self) -> Command {