cli: move following behaviour to `rad follow`

To keep behaviour consistently under subcommands, move the `rad node
following` behaviour under `rad follow`. When an `<nid>` isn't
provided then the followed nodes are listed.

This includes filtering by alias, when `--alias` is used.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2023-12-13 19:49:52 +00:00 committed by cloudhead
parent 6baefdb13f
commit f6e2de30ca
No known key found for this signature in database
5 changed files with 92 additions and 57 deletions

View File

@ -44,7 +44,7 @@ $ rad node seeding
If we wish to see which nodes we are following:
```
$ rad node following
$ rad follow
╭───────────────────────────────────────────────────────────────────────────╮
│ DID Alias Policy │
├───────────────────────────────────────────────────────────────────────────┤

View File

@ -7,6 +7,17 @@ $ rad follow did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk --alias ev
✓ Follow policy updated for z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (eve)
```
We can list the followed peers by omitting the DID:
```
$ rad follow
╭───────────────────────────────────────────────────────────────────────────╮
│ DID Alias Policy │
├───────────────────────────────────────────────────────────────────────────┤
│ did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk eve allow │
╰───────────────────────────────────────────────────────────────────────────╯
```
Now let's seed one of Eve's repositories:
```

View File

@ -2,8 +2,9 @@ use std::ffi::OsString;
use anyhow::anyhow;
use radicle::node::{Alias, Handle, NodeId};
use radicle::node::{policy, Alias, AliasStore, Handle, NodeId};
use radicle::{prelude::*, Node};
use radicle_term::{Element as _, Paint, Table};
use crate::terminal as term;
use crate::terminal::args::{Args, Error, Help};
@ -15,10 +16,12 @@ pub const HELP: Help = Help {
usage: r#"
Usage
rad follow <nid> [--alias <name>] [<option>...]
rad follow [<nid>] [--alias <name>] [<option>...]
The `follow` command takes a Node ID, optionally in DID format, and updates the follow
policy for that peer.
The `follow` command will print all nodes being followed, optionally filtered by alias, if no
Node ID is provided.
Otherwise, it takes a Node ID, optionally in DID format, and updates the follow policy
for that peer, optionally giving the peer the alias provided.
Options
@ -28,10 +31,22 @@ Options
"#,
};
#[derive(Debug)]
pub enum Operation {
Follow { nid: NodeId, alias: Option<Alias> },
List { alias: Option<Alias> },
}
#[derive(Debug, Default)]
pub enum OperationName {
Follow,
#[default]
List,
}
#[derive(Debug)]
pub struct Options {
pub nid: NodeId,
pub alias: Option<Alias>,
pub op: Operation,
pub verbose: bool,
}
@ -71,14 +86,11 @@ impl Args for Options {
}
}
Ok((
Options {
nid: nid.ok_or_else(|| anyhow!("a Node ID must be specified"))?,
alias,
verbose,
},
vec![],
))
let op = match nid {
Some(nid) => Operation::Follow { nid, alias },
None => Operation::List { alias },
};
Ok((Options { op, verbose }, vec![]))
}
}
@ -86,7 +98,10 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let profile = ctx.profile()?;
let mut node = radicle::Node::new(profile.socket());
follow(options.nid, options.alias, &mut node, &profile)?;
match options.op {
Operation::Follow { nid, alias } => follow(nid, alias, &mut node, &profile)?,
Operation::List { alias } => following(&profile, alias)?,
}
Ok(())
}
@ -121,3 +136,52 @@ pub fn follow(
Ok(())
}
pub fn following(profile: &Profile, alias: Option<Alias>) -> anyhow::Result<()> {
let store = profile.policies()?;
let aliases = profile.aliases();
let mut t = term::Table::new(term::table::TableOptions::bordered());
t.push([
term::format::default(String::from("DID")),
term::format::default(String::from("Alias")),
term::format::default(String::from("Policy")),
]);
t.divider();
match alias {
None => push_policies(&mut t, &aliases, store.follow_policies()?),
Some(alias) => push_policies(
&mut t,
&aliases,
store
.follow_policies()?
.filter(|p| p.alias.as_ref().is_some_and(|alias_| *alias_ == alias)),
),
};
t.print();
Ok(())
}
fn push_policies(
t: &mut Table<3, Paint<String>>,
aliases: &impl AliasStore,
policies: impl Iterator<Item = policy::Node>,
) {
for policy::Node { id, alias, policy } in policies {
t.push([
term::format::highlight(Did::from(id).to_string()),
match alias {
None => term::format::secondary(fallback_alias(&id, aliases)),
Some(alias) => term::format::secondary(alias.to_string()),
},
term::format::secondary(policy.to_string()),
]);
}
}
fn fallback_alias(nid: &PublicKey, aliases: &impl AliasStore) -> String {
aliases
.alias(nid)
.map_or("n/a".to_string(), |alias| alias.to_string())
}

View File

@ -32,7 +32,6 @@ Usage
rad node logs [-n <lines>]
rad node connect <nid>@<addr> [<option>...]
rad node routing [--rid <rid>] [--nid <nid>] [--json] [<option>...]
rad node following [<option>...]
rad node seeding [<option>...]
rad node events [--timeout <secs>] [-n <count>] [<option>...]
rad node config
@ -91,7 +90,6 @@ pub enum Operation {
Status,
Sessions,
Stop,
Following,
Seeding,
}
@ -107,7 +105,6 @@ pub enum OperationName {
Status,
Sessions,
Stop,
Following,
Seeding,
}
@ -143,7 +140,6 @@ impl Args for Options {
"status" => op = Some(OperationName::Status),
"stop" => op = Some(OperationName::Stop),
"seeding" => op = Some(OperationName::Seeding),
"following" => op = Some(OperationName::Following),
"sessions" => op = Some(OperationName::Sessions),
unknown => anyhow::bail!("unknown operation '{}'", unknown),
@ -206,7 +202,6 @@ impl Args for Options {
OperationName::Sessions => Operation::Sessions,
OperationName::Stop => Operation::Stop,
OperationName::Seeding => Operation::Seeding,
OperationName::Following => Operation::Following,
};
Ok((Options { op }, vec![]))
}
@ -249,7 +244,6 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
control::stop(node)?;
}
Operation::Seeding => policies::seeding(&profile)?,
Operation::Following => policies::following(&profile)?,
}
Ok(())

View File

@ -1,6 +1,4 @@
use radicle::crypto::PublicKey;
use radicle::node::{policy, AliasStore};
use radicle::prelude::Did;
use radicle::node::policy;
use radicle::Profile;
use crate::terminal as term;
@ -31,35 +29,3 @@ pub fn seeding(profile: &Profile) -> anyhow::Result<()> {
Ok(())
}
pub fn following(profile: &Profile) -> anyhow::Result<()> {
let store = profile.policies()?;
let aliases = profile.aliases();
let mut t = term::Table::new(term::table::TableOptions::bordered());
t.push([
term::format::default(String::from("DID")),
term::format::default(String::from("Alias")),
term::format::default(String::from("Policy")),
]);
t.divider();
for policy::Node { id, alias, policy } in store.follow_policies()? {
t.push([
term::format::highlight(Did::from(id).to_string()),
match alias {
None => term::format::secondary(fallback_alias(&id, &aliases)),
Some(alias) => term::format::secondary(alias.to_string()),
},
term::format::secondary(policy.to_string()),
]);
}
t.print();
Ok(())
}
fn fallback_alias(nid: &PublicKey, aliases: &impl AliasStore) -> String {
aliases
.alias(nid)
.map_or("n/a".to_string(), |alias| alias.to_string())
}