cli: Use alias fallback in `rad node`

The `AliasStore` mechanism was added but not used in the `rad node`
command. Since the launched node itself can display aliases based on
the address book, it would make sense if the `rad node` command could
do this as well.

Use the `Profile`'s `AliasStore` implementation to fallback to an
alias if the tracking store does not contain an alias -- essentially
using the address book instead.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2023-10-24 11:43:16 +01:00 committed by cloudhead
parent c82344496b
commit d9be25a477
No known key found for this signature in database
2 changed files with 22 additions and 14 deletions

View File

@ -3,7 +3,7 @@ use std::time;
use anyhow::anyhow;
use radicle::node::{Address, Node, NodeId, PeerAddr, ROUTING_DB_FILE, TRACKING_DB_FILE};
use radicle::node::{Address, Node, NodeId, PeerAddr, ROUTING_DB_FILE};
use radicle::prelude::Id;
use crate::terminal as term;
@ -185,7 +185,7 @@ impl Args for Options {
tracking_mode = TrackingMode::Repos
}
Long("nodes") if matches!(op, Some(OperationName::Tracking)) => {
tracking_mode = TrackingMode::Nodes
tracking_mode = TrackingMode::Nodes;
}
Long("foreground") if matches!(op, Some(OperationName::Start)) => {
foreground = true;
@ -267,12 +267,7 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
Operation::Stop => {
control::stop(node)?;
}
Operation::Tracking { mode } => {
let store = radicle::node::tracking::store::Config::reader(
profile.home.node().join(TRACKING_DB_FILE),
)?;
tracking::run(&store, mode)?
}
Operation::Tracking { mode } => tracking::run(&profile, mode)?,
}
Ok(())

View File

@ -1,15 +1,19 @@
use radicle::node::tracking;
use radicle::crypto::PublicKey;
use radicle::node::{tracking, AliasStore, TRACKING_DB_FILE};
use radicle::prelude::Did;
use radicle::Profile;
use crate::terminal as term;
use term::Element;
use super::TrackingMode;
pub fn run(store: &tracking::store::ConfigReader, mode: TrackingMode) -> anyhow::Result<()> {
pub fn run(profile: &Profile, mode: TrackingMode) -> anyhow::Result<()> {
let store =
radicle::node::tracking::store::Config::reader(profile.home.node().join(TRACKING_DB_FILE))?;
match mode {
TrackingMode::Repos => print_repos(store)?,
TrackingMode::Nodes => print_nodes(store)?,
TrackingMode::Repos => print_repos(&store)?,
TrackingMode::Nodes => print_nodes(&store, &profile.aliases())?,
}
Ok(())
}
@ -39,7 +43,10 @@ fn print_repos(store: &tracking::store::ConfigReader) -> anyhow::Result<()> {
Ok(())
}
fn print_nodes(store: &tracking::store::ConfigReader) -> anyhow::Result<()> {
fn print_nodes(
store: &tracking::store::ConfigReader,
aliases: &impl AliasStore,
) -> anyhow::Result<()> {
let mut t = term::Table::new(term::table::TableOptions::bordered());
t.push([
term::format::default(String::from("DID")),
@ -52,7 +59,7 @@ fn print_nodes(store: &tracking::store::ConfigReader) -> anyhow::Result<()> {
t.push([
term::format::highlight(Did::from(id).to_string()),
match alias {
None => term::format::secondary(String::from("n/a")),
None => term::format::secondary(fallback_alias(&id, aliases)),
Some(alias) => term::format::secondary(alias.to_string()),
},
term::format::secondary(policy.to_string()),
@ -62,3 +69,9 @@ fn print_nodes(store: &tracking::store::ConfigReader) -> anyhow::Result<()> {
Ok(())
}
fn fallback_alias(nid: &PublicKey, aliases: &impl AliasStore) -> String {
aliases
.alias(nid)
.map_or("n/a".to_string(), |alias| alias.to_string())
}