Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli: Use alias fallback in `rad node`
Fintan Halpenny committed 2 years ago
commit d9be25a477a0cc75862e17348ded54ad7a4c6d4b
parent c82344496bb53a3c1151d3bea617e7bbaa4c920e
2 files changed +22 -14
modified 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(())
modified 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())
+
}