From d9be25a477a0cc75862e17348ded54ad7a4c6d4b Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Tue, 24 Oct 2023 11:43:16 +0100 Subject: [PATCH] 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 X-Clacks-Overhead: GNU Terry Pratchett --- radicle-cli/src/commands/node.rs | 11 +++------- radicle-cli/src/commands/node/tracking.rs | 25 +++++++++++++++++------ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/radicle-cli/src/commands/node.rs b/radicle-cli/src/commands/node.rs index 1f5b4718..e364e770 100644 --- a/radicle-cli/src/commands/node.rs +++ b/radicle-cli/src/commands/node.rs @@ -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(()) diff --git a/radicle-cli/src/commands/node/tracking.rs b/radicle-cli/src/commands/node/tracking.rs index 3e80b59e..925b13ff 100644 --- a/radicle-cli/src/commands/node/tracking.rs +++ b/radicle-cli/src/commands/node/tracking.rs @@ -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()) +}