Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
remote-helper: create NodeSession trait and Real impl
Adrian Duke committed 2 months ago
commit 8048df940746dfce01f406c80d01a3e1df3f7ac6
parent 1663d29e1f1383c0cbbd1d276a0c20469c119fa2
1 file changed +93 -0
modified crates/radicle-remote-helper/src/service.rs
@@ -1,9 +1,15 @@
use std::io;
+
use std::io::IsTerminal;
use std::path::Path;
use std::process;

+
use radicle::explorer::ExplorerResource;
use radicle::git;
+
use radicle::node::Handle;
use radicle::storage;
+
use radicle::Profile;
+
use radicle_cli::node::{SyncError, SyncReporting, SyncSettings};
+
use radicle_cli::terminal as term;

/// Abstraction for Git subprocess calls.
pub trait GitService {
@@ -46,3 +52,90 @@ impl GitService for RealGitService {
        git::run(working, args)
    }
}
+

+
/// Abstraction for Node interaction.
+
pub trait NodeSession {
+
    fn is_running(&self) -> bool;
+

+
    fn sync(
+
        &mut self,
+
        repo: &storage::git::Repository,
+
        updated: Vec<ExplorerResource>,
+
        opts: crate::Options,
+
        profile: &Profile,
+
    ) -> Result<(), SyncError>;
+
}
+

+
pub struct RealNodeSession {
+
    node: radicle::Node,
+
}
+

+
impl RealNodeSession {
+
    pub fn new(profile: &Profile) -> Self {
+
        Self {
+
            node: radicle::Node::new(profile.socket()),
+
        }
+
    }
+
}
+

+
impl NodeSession for RealNodeSession {
+
    fn is_running(&self) -> bool {
+
        self.node.is_running()
+
    }
+

+
    fn sync(
+
        &mut self,
+
        repo: &storage::git::Repository,
+
        updated: Vec<ExplorerResource>,
+
        opts: crate::Options,
+
        profile: &Profile,
+
    ) -> Result<(), SyncError> {
+
        let progress = if io::stderr().is_terminal() {
+
            term::PaintTarget::Stderr
+
        } else {
+
            term::PaintTarget::Hidden
+
        };
+

+
        let result = radicle_cli::node::announce(
+
            repo,
+
            SyncSettings::default().with_profile(profile),
+
            SyncReporting {
+
                progress,
+
                completion: term::PaintTarget::Stderr,
+
                debug: opts.sync_debug,
+
            },
+
            &mut self.node,
+
            profile,
+
        )?;
+

+
        let mut urls = Vec::new();
+

+
        if let Some(result) = result {
+
            for seed in profile.config.preferred_seeds.iter() {
+
                if result.is_synced(&seed.id) {
+
                    for resource in updated {
+
                        let url = profile
+
                            .config
+
                            .public_explorer
+
                            .url(seed.addr.host.clone(), repo.id)
+
                            .resource(resource);
+

+
                        urls.push(url);
+
                    }
+
                    break;
+
                }
+
            }
+
        }
+

+
        // Print URLs to the updated resources.
+
        if !urls.is_empty() {
+
            eprintln!();
+
            for url in urls {
+
                eprintln!("  {}", term::format::dim(url));
+
            }
+
            eprintln!();
+
        }
+

+
        Ok(())
+
    }
+
}