radicle_cli/commands/node/
routing.rs1use radicle::node;
2use radicle::prelude::{NodeId, RepoId};
3
4use crate::terminal as term;
5use crate::terminal::Element;
6
7pub fn run<S: node::routing::Store>(
8 routing: &S,
9 rid: Option<RepoId>,
10 nid: Option<NodeId>,
11 json: bool,
12) -> anyhow::Result<()> {
13 let entries = routing.entries()?.filter(|(rid_, nid_)| {
15 (nid.is_none() || Some(nid_) == nid.as_ref())
16 && (rid.is_none() || Some(rid_) == rid.as_ref())
17 });
18
19 if json {
20 print_json(entries);
21 } else {
22 print_table(entries);
23 }
24
25 Ok(())
26}
27
28fn print_table(entries: impl IntoIterator<Item = (RepoId, NodeId)>) {
29 let mut t = term::Table::new(term::table::TableOptions::bordered());
30 t.header([
31 term::format::default(String::from("RID")),
32 term::format::default(String::from("NID")),
33 ]);
34 t.divider();
35
36 for (rid, nid) in entries {
37 t.push([
38 term::format::highlight(rid.to_string()),
39 term::format::node_id_human(&nid),
40 ]);
41 }
42 t.print();
43}
44
45fn print_json(entries: impl IntoIterator<Item = (RepoId, NodeId)>) {
46 for (rid, nid) in entries {
47 println!("{}", serde_json::json!({ "rid": rid, "nid": nid }));
48 }
49}