Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
refactor: Config::default_adapter returns an adapter, not its config
Lars Wirzenius committed 1 year ago
commit e55a7de242448000b97b91b8b287758c2c57bf10
parent 9418e98863e107b1f3c9358fb00278539d01cc4d
2 files changed +14 -12
modified src/bin/cib.rs
@@ -13,7 +13,6 @@ use clap::Parser;
use radicle::Profile;

use radicle_ci_broker::{
-
    adapter::Adapter,
    broker::{Broker, BrokerError},
    config::{Config, ConfigError},
    db::{Db, DbError},
@@ -173,10 +172,7 @@ impl QueuedCmd {
    fn run(&self, args: &Args, config: &Config) -> Result<(), CibError> {
        let profile = Profile::load().map_err(CibError::profile)?;

-
        let spec = config.default_adapter()?;
-
        let adapter = Adapter::new(&spec.command)
-
            .with_environment(spec.envs())
-
            .with_sensitive_environment(spec.sensitive_envs());
+
        let adapter = config.default_adapter()?;
        logger::adapter_config(config);

        let broker = Broker::new(config.db(), config.max_run_time(), &adapter)
@@ -264,11 +260,7 @@ impl ProcessEventsCmd {
            false,
        );

-
        let spec = config.default_adapter()?;
-
        let adapter = Adapter::new(&spec.command)
-
            .with_environment(spec.envs())
-
            .with_sensitive_environment(spec.sensitive_envs());
-
        logger::adapter_config(config);
+
        let adapter = config.default_adapter()?;

        let broker = Broker::new(config.db(), config.max_run_time(), &adapter)
            .map_err(CibError::new_broker)?;
modified src/config.rs
@@ -10,7 +10,7 @@ use std::{
use duration_str::deserialize_duration;
use serde::{Deserialize, Serialize};

-
use crate::{filter::EventFilter, sensitive::Sensitive};
+
use crate::{adapter::Adapter, filter::EventFilter, logger, sensitive::Sensitive};

const DEFAULT_MAX_RUN_TIME: Duration = Duration::from_secs(3600);
const DEFAULT_STATUS_PAGE_UPDATE_INTERVAL: u64 = 10;
@@ -56,9 +56,11 @@ impl Config {
        &self.filters
    }

-
    pub fn default_adapter(&self) -> Result<&AdapterConfig, ConfigError> {
+
    pub fn default_adapter(&self) -> Result<Adapter, ConfigError> {
+
        logger::adapter_config(self);
        self.adapters
            .get(&self.default_adapter)
+
            .map(Adapter::from)
            .ok_or(ConfigError::NoDefaultAdapter)
    }

@@ -117,6 +119,14 @@ impl AdapterConfig {
    }
}

+
impl From<&AdapterConfig> for Adapter {
+
    fn from(config: &AdapterConfig) -> Self {
+
        Self::new(&config.command)
+
            .with_environment(config.envs())
+
            .with_sensitive_environment(config.sensitive_envs())
+
    }
+
}
+

/// All possible errors from configuration handling.
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {