Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
feat: add helper program to show events after filtering
Lars Wirzenius committed 2 years ago
commit 176235883e225baca2c65dacd8aa328e4b38d533
parent c7153deb3f885ef806066ac7843714168b0ef955
1 file changed +98 -0
added src/bin/filter-events.rs
@@ -0,0 +1,98 @@
+
//! Show broker events that are allowed by a filter. This is meant to
+
//! be helpful for testing a filter configuration.
+

+
#![allow(unused_imports)]
+
#![allow(unused_variables)]
+
#![allow(dead_code)]
+

+
use std::{
+
    collections::HashMap,
+
    error::Error,
+
    ffi::OsStr,
+
    io::BufReader,
+
    path::{Path, PathBuf},
+
    process::{Command, Stdio},
+
};
+

+
use log::{debug, info};
+
use serde::{Deserialize, Serialize};
+

+
use radicle::prelude::{Id, Profile};
+
use radicle_ci_broker::{
+
    error::BrokerError,
+
    event::NodeEventSource,
+
    filter::{BrokerEvent, EventFilter},
+
    msg::{Request, Response, RunResult},
+
};
+
use radicle_git_ext::Oid;
+

+
fn main() {
+
    if let Err(e) = fallible_main() {
+
        eprintln!("ERROR: {}", e);
+
        let mut e = e.source();
+
        while let Some(source) = e {
+
            eprintln!("caused by: {}", source);
+
            e = source.source();
+
        }
+
    }
+
}
+

+
fn fallible_main() -> Result<(), BrokerError> {
+
    pretty_env_logger::init();
+
    info!("filter-events starts");
+

+
    let mut args = std::env::args().skip(1);
+
    let filename: PathBuf = if let Some(filename) = args.next() {
+
        PathBuf::from(filename)
+
    } else {
+
        return Err(BrokerError::Usage);
+
    };
+

+
    let config = Config::load(&filename)?;
+
    debug!("loaded configuration: {:#?}", config);
+

+
    let profile = Profile::load()?;
+
    let mut source = NodeEventSource::new(profile)?;
+
    for filter in config.filters.iter() {
+
        source.allow(filter.clone());
+
    }
+

+
    // This loop ends when there's an error, e.g., failure to read an
+
    // event from the node.
+
    loop {
+
        for e in source.event()? {
+
            println!("{:#?}", e);
+
        }
+
    }
+
}
+

+
#[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()))
+
    }
+
}