Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
cli/unfollow: Improvements around NID / DID parsing
Draft did:key:z6MkgFq6...nBGz opened 6 months ago

This patch improves a few things:

  1. simpler error message when NID / DID parsing fails, also adds example values to help
  2. add a CLI test example that showcases rad unfollow
  3. add unit tests around NID / DID parsing
4 files changed +76 -13 d9ce078d 6562e5c4
added crates/radicle-cli/examples/rad-unfollow.md
@@ -0,0 +1,24 @@
+
Let's assume we starting following a remote node we knew about, and aliased it to "eve":
+

+
```
+
$ rad follow did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk --alias eve
+
✓ Follow policy updated for z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (eve)
+
```
+

+
We can list the followed peers:
+

+
```
+
$ rad follow
+
╭───────────────────────────────────────────────────────────────────────────╮
+
│ DID                                                        Alias   Policy │
+
├───────────────────────────────────────────────────────────────────────────┤
+
│ did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk   eve     allow  │
+
╰───────────────────────────────────────────────────────────────────────────╯
+
```
+

+
If we decide to not follow this remote node anymore, we can just do so.
+

+
```
+
$ rad unfollow did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk
+
✓ Follow policy for z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk removed
+
```
modified crates/radicle-cli/src/commands/unfollow.rs
@@ -25,3 +25,36 @@ pub fn run(options: Args, ctx: impl term::Context) -> anyhow::Result<()> {
    }
    Ok(())
}
+

+
#[cfg(test)]
+
mod test {
+
    use clap::error::ErrorKind;
+
    use clap::Parser;
+

+
    use super::Args;
+

+
    #[test]
+
    fn should_parse_nid() {
+
        let args = Args::try_parse_from([
+
            "unfollow",
+
            "z6MksmpU5b1dS7oaqF2bHXhQi1DWy2hB7Mh9CuN7y1DN6QSz",
+
        ]);
+
        assert!(args.is_ok())
+
    }
+

+
    #[test]
+
    fn should_parse_did() {
+
        let args = Args::try_parse_from([
+
            "unfollow",
+
            "did:key:z6MksmpU5b1dS7oaqF2bHXhQi1DWy2hB7Mh9CuN7y1DN6QSz",
+
        ]);
+
        assert!(args.is_ok())
+
    }
+

+
    #[test]
+
    fn should_not_parse() {
+
        let err = Args::try_parse_from(["unfollow", "bee"]).unwrap_err();
+

+
        assert_eq!(err.kind(), ErrorKind::ValueValidation);
+
    }
+
}
modified crates/radicle-cli/src/commands/unfollow/args.rs
@@ -12,25 +12,26 @@ The `unfollow` command takes a Node ID, optionally in DID format,
and removes the follow policy for that peer."#;

#[derive(Debug, Error)]
-
#[error("invalid Node ID specified (Node ID parsing failed with: '{nid}', DID parsing failed with: '{did}'))")]
-
struct NodeIdParseError {
-
    did: radicle::identity::did::DidError,
-
    nid: radicle::crypto::PublicKeyError,
-
}
-

-
fn parse_nid(value: &str) -> Result<NodeId, NodeIdParseError> {
-
    value.parse::<Did>().map(NodeId::from).or_else(|did| {
-
        value
-
            .parse::<NodeId>()
-
            .map_err(|nid| NodeIdParseError { nid, did })
-
    })
+
#[error("could not parse value as Node ID or Node ID in DID format")]
+
struct NodeIdParserError {}
+

+
fn parse_nid(value: &str) -> Result<NodeId, NodeIdParserError> {
+
    value
+
        .parse::<Did>()
+
        .map(NodeId::from)
+
        .or(value.parse::<NodeId>())
+
        .map_err(|_| NodeIdParserError {})
}

#[derive(Debug, Parser)]
#[command(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)]
pub struct Args {
    /// Node ID (optionally in DID format) of the peer to unfollow
-
    #[arg(value_name = "NID", value_parser = parse_nid)]
+
    ///
+
    /// Example values:
+
    /// - z6MksmpU5b1dS7oaqF2bHXhQi1DWy2hB7Mh9CuN7y1DN6QSz
+
    /// - did:key:z6MksmpU5b1dS7oaqF2bHXhQi1DWy2hB7Mh9CuN7y1DN6QSz
+
    #[arg(value_name = "NID", value_parser = parse_nid, verbatim_doc_comment)]
    pub(super) nid: NodeId,

    /// Verbose output
modified crates/radicle-cli/tests/commands.rs
@@ -1216,6 +1216,11 @@ fn rad_unseed_many() {
}

#[test]
+
fn rad_unfollow() {
+
    Environment::alice(["rad-unfollow"]);
+
}
+

+
#[test]
fn rad_block() {
    let mut environment = Environment::new();
    let alice = environment.node_with(Config {