signing: propagate config errors

This commit is contained in:
Yuya Nishihara 2024-02-21 21:53:03 +09:00
parent 5e13a4a719
commit 7a7962bb9f
3 changed files with 32 additions and 24 deletions

View file

@ -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
pub fn from_config(config: &config::Config) -> Result<Self, config::ConfigError> {
let program = config
.get_string("signing.backends.gpg.program")
.unwrap_or_else(|_| "gpg".into())
.into(),
config
.optional()?
.unwrap_or_else(|| "gpg".into());
let allow_expired_keys = config
.get_bool("signing.backends.gpg.allow-expired-keys")
.unwrap_or(false),
)
.optional()?
.unwrap_or(false);
Ok(Self::new(program.into(), allow_expired_keys))
}
fn create_command(&self) -> Command {

View file

@ -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<Self, SignInitError> {
let mut backends = vec![
Box::new(GpgBackend::from_config(settings.config())) as Box<dyn SigningBackend>,
Box::new(SshBackend::from_config(settings.config())) as Box<dyn SigningBackend>,
// Box::new(X509Backend::from_settings(settings)?) as Box<dyn SigningBackend>,
let mut backends: Vec<Box<dyn SigningBackend>> = 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

View file

@ -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
pub fn from_config(config: &config::Config) -> Result<Self, config::ConfigError> {
let program = config
.get_string("signing.backends.ssh.program")
.unwrap_or_else(|_| "ssh-keygen".into())
.into(),
config
.optional()?
.unwrap_or_else(|| "ssh-keygen".into());
let allowed_signers = config
.get_string("signing.backends.ssh.allowed-signers")
.map_or(None, |v| Some(v.into())),
)
.optional()?;
Ok(Self::new(program.into(), allowed_signers.map(|v| v.into())))
}
fn create_command(&self) -> Command {