Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli: move seeding behaviour to `rad seed`
Fintan Halpenny committed 2 years ago
commit e3b3f3c4f4fd1f8438f37dc1742a7ad9a4c4652a
parent f6e2de30ca40402aedc8c062ef0bf59e05f51919
5 files changed +64 -64
modified radicle-cli/examples/rad-node.md
@@ -28,12 +28,12 @@ $ rad follow did:key:z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk --alias Bo
✓ Follow policy updated for z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (Bob)
```

-
Now, when we use the `rad node seeding` command we will see
+
Now, when we use the `rad seed` command we will see
information for repositories that we seed -- in this case a
repository that was already created:

```
-
$ rad node seeding
+
$ rad seed
╭───────────────────────────────────────────────────────╮
│ RID                                 Scope      Policy │
├───────────────────────────────────────────────────────┤
modified radicle-cli/examples/rad-seed-and-follow.md
@@ -24,3 +24,14 @@ Now let's seed one of Eve's repositories:
$ rad seed rad:z42hL2jL4XNk6K8oHQaSWfMgCL7ji --scope followed --no-fetch
✓ Seeding policy updated for rad:z42hL2jL4XNk6K8oHQaSWfMgCL7ji with scope 'followed'
```
+

+
We can list the repositories we are seeding by omitting the RID:
+

+
```
+
$ rad seed
+
╭───────────────────────────────────────────────────────╮
+
│ RID                                 Scope      Policy │
+
├───────────────────────────────────────────────────────┤
+
│ rad:z42hL2jL4XNk6K8oHQaSWfMgCL7ji   followed   allow  │
+
╰───────────────────────────────────────────────────────╯
+
```
modified radicle-cli/src/commands/node.rs
@@ -13,9 +13,7 @@ use crate::terminal::Element as _;
#[path = "node/control.rs"]
pub mod control;
#[path = "node/events.rs"]
-
pub mod events;
-
#[path = "node/policies.rs"]
-
pub mod policies;
+
mod events;
#[path = "node/routing.rs"]
pub mod routing;

@@ -32,7 +30,6 @@ Usage
    rad node logs [-n <lines>]
    rad node connect <nid>@<addr> [<option>...]
    rad node routing [--rid <rid>] [--nid <nid>] [--json] [<option>...]
-
    rad node seeding [<option>...]
    rad node events [--timeout <secs>] [-n <count>] [<option>...]
    rad node config

@@ -90,7 +87,6 @@ pub enum Operation {
    Status,
    Sessions,
    Stop,
-
    Seeding,
}

#[derive(Default, PartialEq, Eq)]
@@ -105,7 +101,6 @@ pub enum OperationName {
    Status,
    Sessions,
    Stop,
-
    Seeding,
}

impl Args for Options {
@@ -139,7 +134,6 @@ impl Args for Options {
                    "start" => op = Some(OperationName::Start),
                    "status" => op = Some(OperationName::Status),
                    "stop" => op = Some(OperationName::Stop),
-
                    "seeding" => op = Some(OperationName::Seeding),
                    "sessions" => op = Some(OperationName::Sessions),

                    unknown => anyhow::bail!("unknown operation '{}'", unknown),
@@ -201,7 +195,6 @@ impl Args for Options {
            OperationName::Status => Operation::Status,
            OperationName::Sessions => Operation::Sessions,
            OperationName::Stop => Operation::Stop,
-
            OperationName::Seeding => Operation::Seeding,
        };
        Ok((Options { op }, vec![]))
    }
@@ -243,7 +236,6 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
        Operation::Stop => {
            control::stop(node)?;
        }
-
        Operation::Seeding => policies::seeding(&profile)?,
    }

    Ok(())
deleted radicle-cli/src/commands/node/policies.rs
@@ -1,31 +0,0 @@
-
use radicle::node::policy;
-
use radicle::Profile;
-

-
use crate::terminal as term;
-
use term::Element;
-

-
pub fn seeding(profile: &Profile) -> anyhow::Result<()> {
-
    let store = profile.policies()?;
-
    let mut t = term::Table::new(term::table::TableOptions::bordered());
-
    t.push([
-
        term::format::default(String::from("RID")),
-
        term::format::default(String::from("Scope")),
-
        term::format::default(String::from("Policy")),
-
    ]);
-
    t.divider();
-

-
    for policy::Repo { id, scope, policy } in store.seed_policies()? {
-
        let id = id.to_string();
-
        let scope = scope.to_string();
-
        let policy = policy.to_string();
-

-
        t.push([
-
            term::format::highlight(id),
-
            term::format::secondary(scope),
-
            term::format::secondary(policy),
-
        ])
-
    }
-
    t.print();
-

-
    Ok(())
-
}
modified radicle-cli/src/commands/seed.rs
@@ -3,9 +3,11 @@ use std::time;

use anyhow::anyhow;

+
use radicle::node::policy;
use radicle::node::policy::Scope;
use radicle::node::Handle;
use radicle::{prelude::*, Node};
+
use radicle_term::Element as _;

use crate::commands::rad_sync as sync;
use crate::terminal::args::{Args, Error, Help};
@@ -18,10 +20,13 @@ pub const HELP: Help = Help {
    usage: r#"
Usage

-
    rad seed <rid> [-d | --delete] [--[no-]fetch] [--scope <scope>] [<option>...]
+
    rad seed [<rid>] [-d | --delete] [--[no-]fetch] [--scope <scope>] [<option>...]

-
    The `seed` command takes a Repository ID (<rid>) and updates the seeding policy
-
    for that repository. By default, a seeding policy will be created or updated.
+
    The `seed` command, when no Repository ID (<rid>) is provided, will list the
+
    repositories being seeded.
+

+
    When a Repository ID (<rid>) is provided it updates the seeding policy for
+
    that repository. By default, a seeding policy will be created or updated.
    To delete a policy, use the `--delete` flag.

    When seeding a repository, a scope can be specified: this can be either `all` or
@@ -41,8 +46,9 @@ Options

#[derive(Debug)]
pub enum Operation {
-
    Seed { fetch: bool, scope: Scope },
-
    Unseed,
+
    Seed { rid: Id, fetch: bool, scope: Scope },
+
    List,
+
    Unseed { rid: Id },
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
@@ -54,7 +60,6 @@ pub enum OperationName {

#[derive(Debug)]
pub struct Options {
-
    pub rid: Id,
    pub op: Operation,
    pub verbose: bool,
}
@@ -98,33 +103,29 @@ impl Args for Options {
            }
        }

-
        let op = match op.unwrap_or_default() {
-
            OperationName::Seed => Operation::Seed {
-
                fetch: fetch.unwrap_or(true),
-
                scope: scope.unwrap_or(Scope::All),
+
        let op = match rid {
+
            Some(rid) => match op.unwrap_or_default() {
+
                OperationName::Seed => Operation::Seed {
+
                    rid,
+
                    fetch: fetch.unwrap_or(true),
+
                    scope: scope.unwrap_or(Scope::All),
+
                },
+
                OperationName::Unseed => Operation::Unseed { rid },
            },
-
            OperationName::Unseed => Operation::Unseed,
+
            None => Operation::List,
        };

-
        Ok((
-
            Options {
-
                rid: rid.ok_or_else(|| anyhow!("a Repository ID must be specified"))?,
-
                op,
-
                verbose,
-
            },
-
            vec![],
-
        ))
+
        Ok((Options { op, verbose }, vec![]))
    }
}

pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
    let profile = ctx.profile()?;
    let mut node = radicle::Node::new(profile.socket());
-
    let rid = options.rid;

    match options.op {
-
        Operation::Unseed => delete(rid, &mut node, &profile)?,
-
        Operation::Seed { fetch, scope } => {
+
        Operation::Unseed { rid } => delete(rid, &mut node, &profile)?,
+
        Operation::Seed { rid, fetch, scope } => {
            update(rid, scope, &mut node, &profile)?;

            if fetch && node.is_running() {
@@ -136,6 +137,7 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
                )?;
            }
        }
+
        Operation::List => seeding(&profile)?,
    }

    Ok(())
@@ -164,3 +166,29 @@ pub fn delete(rid: Id, node: &mut Node, profile: &Profile) -> anyhow::Result<()>
    }
    Ok(())
}
+

+
pub fn seeding(profile: &Profile) -> anyhow::Result<()> {
+
    let store = profile.policies()?;
+
    let mut t = term::Table::new(term::table::TableOptions::bordered());
+
    t.push([
+
        term::format::default(String::from("RID")),
+
        term::format::default(String::from("Scope")),
+
        term::format::default(String::from("Policy")),
+
    ]);
+
    t.divider();
+

+
    for policy::Repo { id, scope, policy } in store.seed_policies()? {
+
        let id = id.to_string();
+
        let scope = scope.to_string();
+
        let policy = policy.to_string();
+

+
        t.push([
+
            term::format::highlight(id),
+
            term::format::secondary(scope),
+
            term::format::secondary(policy),
+
        ])
+
    }
+
    t.print();
+

+
    Ok(())
+
}