cli: Add routing entries per rid and or nid

To allow third party tools read the routing table entries filtered by
rid or nid and if needed in json format.

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez
2023-05-22 11:58:43 +02:00
parent 2d55ffba81
commit 76ba25268d
2 changed files with 55 additions and 10 deletions
+31 -5
View File
@@ -1,9 +1,31 @@
use radicle::node;
use radicle::prelude::{Id, NodeId};
use crate::terminal as term;
use crate::terminal::Element;
pub fn run<S: node::routing::Store>(routing: &S) -> anyhow::Result<()> {
pub fn run<S: node::routing::Store>(
routing: &S,
rid: Option<Id>,
nid: Option<NodeId>,
json: bool,
) -> anyhow::Result<()> {
// Filters entries by RID or NID exclusively, or show all of them if none given.
let entries = routing.entries()?.filter(|(rid_, nid_)| {
(nid.is_none() || Some(nid_) == nid.as_ref())
&& (rid.is_none() || Some(rid_) == rid.as_ref())
});
if json {
print_json(entries);
} else {
print_table(entries);
}
Ok(())
}
fn print_table(entries: impl IntoIterator<Item = (Id, NodeId)>) {
let mut t = term::Table::new(term::table::TableOptions::bordered());
t.push([
term::format::default(String::from("RID")),
@@ -11,13 +33,17 @@ pub fn run<S: node::routing::Store>(routing: &S) -> anyhow::Result<()> {
]);
t.divider();
for (id, node) in routing.entries()? {
for (rid, nid) in entries {
t.push([
term::format::highlight(id.to_string()),
term::format::default(term::format::node(&node)),
term::format::highlight(rid.to_string()),
term::format::default(term::format::node(&nid)),
]);
}
t.print();
}
Ok(())
fn print_json(entries: impl IntoIterator<Item = (Id, NodeId)>) {
for (rid, nid) in entries {
println!("{}", serde_json::json!({ "rid": rid, "nid": nid }));
}
}