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: If we wish to see which nodes we are following:
``` ```
$ rad node following $ rad follow
╭───────────────────────────────────────────────────────────────────────────╮ ╭───────────────────────────────────────────────────────────────────────────╮
│ DID Alias Policy │ │ DID Alias Policy │
├───────────────────────────────────────────────────────────────────────────┤ ├───────────────────────────────────────────────────────────────────────────┤

View File

@ -7,6 +7,17 @@ $ rad follow did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk --alias ev
✓ Follow policy updated for z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (eve) ✓ 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: Now let's seed one of Eve's repositories:
``` ```

View File

@ -2,8 +2,9 @@ use std::ffi::OsString;
use anyhow::anyhow; use anyhow::anyhow;
use radicle::node::{Alias, Handle, NodeId}; use radicle::node::{policy, Alias, AliasStore, Handle, NodeId};
use radicle::{prelude::*, Node}; use radicle::{prelude::*, Node};
use radicle_term::{Element as _, Paint, Table};
use crate::terminal as term; use crate::terminal as term;
use crate::terminal::args::{Args, Error, Help}; use crate::terminal::args::{Args, Error, Help};
@ -15,10 +16,12 @@ pub const HELP: Help = Help {
usage: r#" usage: r#"
Usage 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 The `follow` command will print all nodes being followed, optionally filtered by alias, if no
policy for that peer. 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 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)] #[derive(Debug)]
pub struct Options { pub struct Options {
pub nid: NodeId, pub op: Operation,
pub alias: Option<Alias>,
pub verbose: bool, pub verbose: bool,
} }
@ -71,14 +86,11 @@ impl Args for Options {
} }
} }
Ok(( let op = match nid {
Options { Some(nid) => Operation::Follow { nid, alias },
nid: nid.ok_or_else(|| anyhow!("a Node ID must be specified"))?, None => Operation::List { alias },
alias, };
verbose, Ok((Options { op, verbose }, vec![]))
},
vec![],
))
} }
} }
@ -86,7 +98,10 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let profile = ctx.profile()?; let profile = ctx.profile()?;
let mut node = radicle::Node::new(profile.socket()); 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(()) Ok(())
} }
@ -121,3 +136,52 @@ pub fn follow(
Ok(()) 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 logs [-n <lines>]
rad node connect <nid>@<addr> [<option>...] rad node connect <nid>@<addr> [<option>...]
rad node routing [--rid <rid>] [--nid <nid>] [--json] [<option>...] rad node routing [--rid <rid>] [--nid <nid>] [--json] [<option>...]
rad node following [<option>...]
rad node seeding [<option>...] rad node seeding [<option>...]
rad node events [--timeout <secs>] [-n <count>] [<option>...] rad node events [--timeout <secs>] [-n <count>] [<option>...]
rad node config rad node config
@ -91,7 +90,6 @@ pub enum Operation {
Status, Status,
Sessions, Sessions,
Stop, Stop,
Following,
Seeding, Seeding,
} }
@ -107,7 +105,6 @@ pub enum OperationName {
Status, Status,
Sessions, Sessions,
Stop, Stop,
Following,
Seeding, Seeding,
} }
@ -143,7 +140,6 @@ impl Args for Options {
"status" => op = Some(OperationName::Status), "status" => op = Some(OperationName::Status),
"stop" => op = Some(OperationName::Stop), "stop" => op = Some(OperationName::Stop),
"seeding" => op = Some(OperationName::Seeding), "seeding" => op = Some(OperationName::Seeding),
"following" => op = Some(OperationName::Following),
"sessions" => op = Some(OperationName::Sessions), "sessions" => op = Some(OperationName::Sessions),
unknown => anyhow::bail!("unknown operation '{}'", unknown), unknown => anyhow::bail!("unknown operation '{}'", unknown),
@ -206,7 +202,6 @@ impl Args for Options {
OperationName::Sessions => Operation::Sessions, OperationName::Sessions => Operation::Sessions,
OperationName::Stop => Operation::Stop, OperationName::Stop => Operation::Stop,
OperationName::Seeding => Operation::Seeding, OperationName::Seeding => Operation::Seeding,
OperationName::Following => Operation::Following,
}; };
Ok((Options { op }, vec![])) Ok((Options { op }, vec![]))
} }
@ -249,7 +244,6 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
control::stop(node)?; control::stop(node)?;
} }
Operation::Seeding => policies::seeding(&profile)?, Operation::Seeding => policies::seeding(&profile)?,
Operation::Following => policies::following(&profile)?,
} }
Ok(()) Ok(())

View File

@ -1,6 +1,4 @@
use radicle::crypto::PublicKey; use radicle::node::policy;
use radicle::node::{policy, AliasStore};
use radicle::prelude::Did;
use radicle::Profile; use radicle::Profile;
use crate::terminal as term; use crate::terminal as term;
@ -31,35 +29,3 @@ pub fn seeding(profile: &Profile) -> anyhow::Result<()> {
Ok(()) 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())
}