Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
refactor: move config stuff into src/config.rs
Lars Wirzenius committed 2 years ago
commit 6c5012453ef362b6e687999f3326a94db1859c1d
parent a4047dc96833ae628262f95401ecbdbdac78bb60
4 files changed +45 -62
modified src/bin/ci-broker.rs
@@ -16,6 +16,7 @@ use serde::{Deserialize, Serialize};

use radicle::prelude::{Id, Profile};
use radicle_ci_broker::{
+
    config::{Adapter, Config},
    error::BrokerError,
    event::NodeEventSource,
    filter::{BrokerEvent, EventFilter},
@@ -125,34 +126,3 @@ fn run(adapter: &Adapter, repo: Id, commit: Oid) -> Result<Option<RunResult>, Br

    Ok(ret)
}
-

-
#[derive(Debug, Serialize, Deserialize)]
-
struct Config {
-
    default_adapter: String,
-
    adapters: HashMap<String, Adapter>,
-
    filters: Vec<EventFilter>,
-
}
-

-
impl Config {
-
    fn load(filename: &Path) -> Result<Self, BrokerError> {
-
        let config =
-
            std::fs::read(filename).map_err(|e| BrokerError::ReadConfig(filename.into(), e))?;
-
        serde_yaml::from_slice(&config).map_err(|e| BrokerError::ParseConfig(filename.into(), e))
-
    }
-

-
    fn adapter(&self, name: &str) -> Option<&Adapter> {
-
        self.adapters.get(name)
-
    }
-
}
-

-
#[derive(Debug, Serialize, Deserialize)]
-
struct Adapter {
-
    command: PathBuf,
-
    env: HashMap<String, String>,
-
}
-

-
impl Adapter {
-
    fn envs(&self) -> impl Iterator<Item = (&OsStr, &OsStr)> {
-
        self.env.iter().map(|(k, v)| (k.as_ref(), v.as_ref()))
-
    }
-
}
modified src/bin/filter-events.rs
@@ -19,6 +19,7 @@ use serde::{Deserialize, Serialize};

use radicle::prelude::{Id, Profile};
use radicle_ci_broker::{
+
    config::Config,
    error::BrokerError,
    event::NodeEventSource,
    filter::{BrokerEvent, EventFilter},
@@ -65,34 +66,3 @@ fn fallible_main() -> Result<(), BrokerError> {
        }
    }
}
-

-
#[derive(Debug, Serialize, Deserialize)]
-
struct Config {
-
    default_adapter: String,
-
    adapters: HashMap<String, Adapter>,
-
    filters: Vec<EventFilter>,
-
}
-

-
impl Config {
-
    fn load(filename: &Path) -> Result<Self, BrokerError> {
-
        let config =
-
            std::fs::read(filename).map_err(|e| BrokerError::ReadConfig(filename.into(), e))?;
-
        serde_yaml::from_slice(&config).map_err(|e| BrokerError::ParseConfig(filename.into(), e))
-
    }
-

-
    fn adapter(&self, name: &str) -> Option<&Adapter> {
-
        self.adapters.get(name)
-
    }
-
}
-

-
#[derive(Debug, Serialize, Deserialize)]
-
struct Adapter {
-
    command: PathBuf,
-
    env: HashMap<String, String>,
-
}
-

-
impl Adapter {
-
    fn envs(&self) -> impl Iterator<Item = (&OsStr, &OsStr)> {
-
        self.env.iter().map(|(k, v)| (k.as_ref(), v.as_ref()))
-
    }
-
}
added src/config.rs
@@ -0,0 +1,42 @@
+
//! Configuration for the CI broker and related programs.
+

+
use std::{
+
    collections::HashMap,
+
    ffi::OsStr,
+
    path::{Path, PathBuf},
+
};
+

+
use serde::{Deserialize, Serialize};
+

+
use crate::{error::BrokerError, filter::EventFilter};
+

+
#[derive(Debug, Serialize, Deserialize)]
+
pub struct Config {
+
    pub default_adapter: String,
+
    pub adapters: HashMap<String, Adapter>,
+
    pub filters: Vec<EventFilter>,
+
}
+

+
impl Config {
+
    pub fn load(filename: &Path) -> Result<Self, BrokerError> {
+
        let config =
+
            std::fs::read(filename).map_err(|e| BrokerError::ReadConfig(filename.into(), e))?;
+
        serde_yaml::from_slice(&config).map_err(|e| BrokerError::ParseConfig(filename.into(), e))
+
    }
+

+
    pub fn adapter(&self, name: &str) -> Option<&Adapter> {
+
        self.adapters.get(name)
+
    }
+
}
+

+
#[derive(Debug, Serialize, Deserialize)]
+
pub struct Adapter {
+
    pub command: PathBuf,
+
    pub env: HashMap<String, String>,
+
}
+

+
impl Adapter {
+
    pub fn envs(&self) -> impl Iterator<Item = (&OsStr, &OsStr)> {
+
        self.env.iter().map(|(k, v)| (k.as_ref(), v.as_ref()))
+
    }
+
}
modified src/lib.rs
@@ -5,6 +5,7 @@
//! node, filter the events, and to communicate with a adapter spawned
//! as a child process.

+
pub mod config;
pub mod error;
pub mod event;
pub mod filter;