mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-21 10:30:41 +08:00
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:
@@ -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 }));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user