Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
refactor: use ergo module to load profile
Lars Wirzenius committed 2 months ago
commit f38a67bf516402553d1d01a27a1ad01372f6e388
parent 82ed0bc
6 files changed +27 -43
modified src/bin/cib.rs
@@ -9,12 +9,12 @@ use std::{
};

use clap::Parser;
-
use radicle::Profile;
use radicle_ci_broker::{
    adapter::AdapterError,
    broker::{Broker, BrokerError},
    config::{Config, ConfigError},
    db::{Db, DbError},
+
    ergo,
    logger::{self, LogLevel},
    notif::{NotificationChannel, NotificationError},
    pages::{PageError, StatusPage},
@@ -193,8 +193,6 @@ struct QueuedCmd {}

impl QueuedCmd {
    fn run(&self, args: &Args, config: &Config) -> Result<(), CibError> {
-
        let profile = Profile::load().map_err(CibError::profile)?;
-

        let adapters = config.to_adapters().map_err(CibError::Adapters)?;
        logger::adapter_config(config);

@@ -232,7 +230,7 @@ impl QueuedCmd {
        let db = args.open_db(config)?;
        let mut page = StatusPage::new(
            run_notifications.rx().map_err(CibError::notification)?,
-
            profile,
+
            ergo::Radicle::new().map_err(CibError::ergo)?,
            db,
            true,
        );
@@ -274,13 +272,11 @@ impl ProcessEventsCmd {
            .map_err(CibError::QueueAdder)?;
        let adder = start_thread(adder);

-
        let profile = Profile::load().map_err(CibError::profile)?;
-

        let db = args.open_db(config)?;

        let mut page = StatusPage::new(
            run_notification.rx().map_err(CibError::notification)?,
-
            profile,
+
            ergo::Radicle::new().map_err(CibError::ergo)?,
            db,
            false,
        );
@@ -334,6 +330,9 @@ impl ProcessEventsCmd {

#[derive(Debug, thiserror::Error)]
enum CibError {
+
    #[error("failed to load ergonomic Radicle wrapper")]
+
    Ergo(#[source] ergo::ErgoError),
+

    #[error("failed to read configuration file {0}")]
    ReadConfig(PathBuf, #[source] ConfigError),

@@ -346,9 +345,6 @@ enum CibError {
    #[error("failed to convert adapters as JSON")]
    AdaptersToJson(PathBuf, #[source] AdapterError),

-
    #[error("failed to look up node profile")]
-
    Profile(#[source] radicle::profile::Error),
-

    #[error("failed to use SQLite database")]
    Db(#[source] DbError),

@@ -394,8 +390,8 @@ impl CibError {
        Self::AdaptersToJson(filename.into(), e)
    }

-
    fn profile(e: radicle::profile::Error) -> Self {
-
        Self::Profile(e)
+
    fn ergo(e: ergo::ErgoError) -> Self {
+
        Self::Ergo(e)
    }

    fn db(e: DbError) -> Self {
modified src/bin/cibtool.rs
@@ -18,13 +18,13 @@ use std::{

use clap::Parser;

-
use radicle::{Profile, git::Oid, prelude::NodeId};
+
use radicle::{git::Oid, prelude::NodeId};

use radicle_ci_broker::{
    broker::BrokerError,
    ci_event::{CiEvent, CiEventError, CiEventV1},
    db::{Db, DbError, QueueId, QueuedCiEvent},
-
    logger,
+
    ergo, logger,
    msg::{RunId, RunResult},
    notif::{NotificationChannel, NotificationError},
    pages::PageError,
@@ -250,12 +250,12 @@ enum RunSubCmd {

#[derive(Debug, thiserror::Error)]
enum CibToolError {
+
    #[error("failed to load ergonomic Radicle wrapper")]
+
    Ergo(#[source] ergo::ErgoError),
+

    #[error("failed to create cache of job COBs")]
    KnownJobCobs(#[source] radicle_ci_broker::cob::JobError),

-
    #[error("failed to look up node profile")]
-
    Profile(#[source] radicle::profile::Error),
-

    #[error("cannot find CI run with id {0}")]
    RunNotFound(RunId),

modified src/bin/cibtoolcmd/event.rs
@@ -130,8 +130,8 @@ impl Leaf for AddEvent {
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
        let r = ergo::Radicle::new().map_err(EventError::Ergonomic)?;

-
        let profile = r.profile();
-
        let nid = *profile.id();
+
        let radicle = ergo::Radicle::new().map_err(CibToolError::Ergo)?;
+
        let nid = *radicle.profile().id();

        let repo = r
            .repository_by_name(&self.repo)
@@ -365,8 +365,9 @@ pub struct RecordEvents {

impl Leaf for RecordEvents {
    fn run(&self, _args: &Args) -> Result<(), CibToolError> {
-
        let profile = util::load_profile()?;
-
        let mut source = NodeEventSource::new(&profile).map_err(CibToolError::EventSubscribe)?;
+
        let radicle = ergo::Radicle::new().map_err(CibToolError::Ergo)?;
+
        let mut source =
+
            NodeEventSource::new(radicle.profile()).map_err(CibToolError::EventSubscribe)?;
        let mut file = if let Some(filename) = &self.output {
            Some(
                std::fs::File::create(filename)
@@ -419,7 +420,7 @@ pub struct CiEvents {

impl Leaf for CiEvents {
    fn run(&self, _args: &Args) -> Result<(), CibToolError> {
-
        let profile = Profile::load().map_err(CibToolError::Profile)?;
+
        let radicle = ergo::Radicle::new().map_err(CibToolError::Ergo)?;

        let bytes = std::fs::read(&self.input)
            .map_err(|e| CibToolError::ReadEvents(self.input.clone(), e))?;
@@ -435,7 +436,7 @@ impl Leaf for CiEvents {

        let mut ci_events: Vec<CiEvent> = vec![];
        for node_event in node_events.iter() {
-
            if let Ok(mut cevs) = CiEvent::from_node_event(node_event, &profile) {
+
            if let Ok(mut cevs) = CiEvent::from_node_event(node_event, radicle.profile()) {
                ci_events.append(&mut cevs);
            }
        }
modified src/bin/cibtoolcmd/report.rs
@@ -1,4 +1,4 @@
-
use radicle_ci_broker::{pages::StatusPage, worker::start_thread};
+
use radicle_ci_broker::{ergo, pages::StatusPage, worker::start_thread};

use super::*;

@@ -18,14 +18,12 @@ pub struct ReportCmd {

impl Leaf for ReportCmd {
    fn run(&self, args: &Args) -> Result<(), CibToolError> {
-
        let profile = Profile::load().map_err(CibToolError::Profile)?;
-

        let db = args.open_db()?;

        let mut run_notification = NotificationChannel::new_run();
        let mut page = StatusPage::new(
            run_notification.rx().map_err(CibToolError::Notification)?,
-
            profile,
+
            ergo::Radicle::new().map_err(CibToolError::Ergo)?,
            db,
            true,
        );
modified src/pages.rs
@@ -28,7 +28,7 @@ use radicle::{
use crate::{
    ci_event::{CiEvent, CiEventV1},
    db::{Db, DbError, QueuedCiEvent},
-
    logger,
+
    ergo, logger,
    msg::{RunId, RunResult},
    notif::NotificationReceiver,
    run::{Run, RunState, Whence},
@@ -863,7 +863,7 @@ pub struct StatusPage {

struct PageArgs {
    run_rx: NotificationReceiver,
-
    profile: Profile,
+
    radicle: ergo::Radicle,
    db: Db,
    once: bool,
    desc_snippet: Option<String>,
@@ -874,13 +874,13 @@ impl StatusPage {
        self.dirname = Some(dirname.into());
    }

-
    pub fn new(run_rx: NotificationReceiver, profile: Profile, db: Db, once: bool) -> Self {
+
    pub fn new(run_rx: NotificationReceiver, radicle: ergo::Radicle, db: Db, once: bool) -> Self {
        Self {
            node_alias: "".into(),
            dirname: None,
            args: PageArgs {
                run_rx,
-
                profile,
+
                radicle,
                db,
                once,
                desc_snippet: None,
@@ -935,7 +935,7 @@ impl StatusPage {
                        | CiEvent::V1(CiEventV1::BranchUpdated { repo, .. })
                        | CiEvent::V1(CiEventV1::PatchCreated { repo, .. })
                        | CiEvent::V1(CiEventV1::PatchUpdated { repo, .. }) => {
-
                            if Self::is_public_repo(&self.args.profile, repo) {
+
                            if Self::is_public_repo(self.args.radicle.profile(), repo) {
                                Some(Ok(event))
                            } else {
                                None
modified src/util.rs
@@ -73,10 +73,6 @@ pub fn oid_from_cli_arg(profile: &Profile, rid: RepoId, commit: &str) -> Result<
    }
}

-
pub fn load_profile() -> Result<Profile, UtilError> {
-
    Profile::load().map_err(UtilError::profile)
-
}
-

pub fn lookup_nid(profile: &Profile) -> Result<NodeId, UtilError> {
    Ok(*profile.id())
}
@@ -160,9 +156,6 @@ pub fn safely_overwrite<P: AsRef<Path>>(filename: P, data: &[u8]) -> Result<(),

#[derive(Debug, thiserror::Error)]
pub enum UtilError {
-
    #[error("failed to look up node profile")]
-
    Profile(#[source] Box<radicle::profile::Error>),
-

    #[error("failed to look up open node storage")]
    Storage(#[source] Box<radicle::storage::Error>),

@@ -216,10 +209,6 @@ pub enum UtilError {
}

impl UtilError {
-
    fn profile(err: radicle::profile::Error) -> Self {
-
        Self::Profile(Box::new(err))
-
    }
-

    fn storage(err: radicle::storage::Error) -> Self {
        Self::Storage(Box::new(err))
    }