Files
heartwood/radicle-cli/src/node.rs
T
Fintan Halpenny 2e781b1efd cli: add control flow for announcement
To allow `rad sync` to break early from the call to `announce`,
control flow is introduced for the callback.

In `rad sync`, the number of synced nodes is checked against the
`replicas` -- if specified -- and sends a `Break`.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
2024-01-10 16:03:14 +01:00

54 lines
1.5 KiB
Rust

use std::ops::ControlFlow;
use std::time::Duration;
use radicle::identity::Id;
use radicle::node;
use radicle::node::Handle as _;
use radicle::Node;
use crate::terminal as term;
/// Announce changes to the network.
pub fn announce(rid: Id, node: &mut Node) -> anyhow::Result<()> {
match announce_(rid, node) {
Ok(()) => Ok(()),
Err(e) if e.is_connection_err() => {
term::hint("Node is stopped. To announce changes to the network, start it with `rad node start`.");
Ok(())
}
Err(e) => Err(e.into()),
}
}
fn announce_(rid: Id, node: &mut Node) -> Result<(), radicle::node::Error> {
let seeds = node.seeds(rid)?;
let connected = seeds.connected().map(|s| s.nid).collect::<Vec<_>>();
if connected.is_empty() {
term::info!("Not connected to any seeds.");
return Ok(());
}
let mut spinner = term::spinner(format!("Syncing with {} node(s)..", connected.len()));
let result = node.announce(
rid,
connected,
Duration::from_secs(9),
|event, _| match event {
node::AnnounceEvent::Announced => ControlFlow::Continue(()),
node::AnnounceEvent::RefsSynced { remote } => {
spinner.message(format!("Synced with {remote}.."));
ControlFlow::Continue(())
}
},
)?;
if result.synced.is_empty() {
spinner.failed();
} else {
spinner.message(format!("Synced with {} node(s)", result.synced.len()));
spinner.finish();
}
Ok(())
}