cli: Add `rad unseed` command

This replaces the `-d` flag of `rad seed`, making it more like `rad
unfollow`.
This commit is contained in:
cloudhead 2024-02-21 17:49:02 +01:00
parent 6b4f055c36
commit eff40166c9
No known key found for this signature in database
6 changed files with 100 additions and 31 deletions

View File

@ -12,7 +12,7 @@ $ rad ls
We could stop seeding it if we didn't want other nodes to fetch it from us:
```
$ rad seed --delete rad:z42hL2jL4XNk6K8oHQaSWfMgCL7ji
$ rad unseed rad:z42hL2jL4XNk6K8oHQaSWfMgCL7ji
✓ Seeding policy for rad:z42hL2jL4XNk6K8oHQaSWfMgCL7ji removed
```

View File

@ -52,5 +52,7 @@ pub mod rad_stats;
pub mod rad_sync;
#[path = "commands/unfollow.rs"]
pub mod rad_unfollow;
#[path = "commands/unseed.rs"]
pub mod rad_unseed;
#[path = "commands/watch.rs"]
pub mod rad_watch;

View File

@ -35,6 +35,7 @@ const COMMANDS: &[Help] = &[
rad_seed::HELP,
rad_follow::HELP,
rad_unfollow::HELP,
rad_unseed::HELP,
rad_remote::HELP,
rad_stats::HELP,
rad_sync::HELP,

View File

@ -20,14 +20,13 @@ pub const HELP: Help = Help {
usage: r#"
Usage
rad seed [<rid>] [-d | --delete] [--[no-]fetch] [--scope <scope>] [<option>...]
rad seed [<rid>] [--[no-]fetch] [--scope <scope>] [<option>...]
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 a Repository ID (<rid>) is provided it updates or creates the seeding policy for
that repository. To delete a seeding policy, use the `rad unseed` command.
When seeding a repository, a scope can be specified: this can be either `all` or
`followed`. When using `all`, all remote nodes will be followed for that repository.
@ -36,7 +35,6 @@ Usage
Options
--delete, -d Delete the seeding policy
--[no-]fetch Fetch repository after updating seeding policy
--scope <scope> Peer follow scope for this repository
--verbose, -v Verbose output
@ -52,16 +50,6 @@ pub enum Operation {
scope: Scope,
},
List,
Unseed {
rid: RepoId,
},
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
pub enum OperationName {
#[default]
Seed,
Unseed,
}
#[derive(Debug)]
@ -78,7 +66,6 @@ impl Args for Options {
let mut rid: Option<RepoId> = None;
let mut scope: Option<Scope> = None;
let mut fetch: Option<bool> = None;
let mut op: Option<OperationName> = None;
let mut verbose = false;
while let Some(arg) = parser.next()? {
@ -86,17 +73,14 @@ impl Args for Options {
Value(val) => {
rid = Some(term::args::rid(val)?);
}
Long("delete") | Short('d') if op.is_none() => {
op = Some(OperationName::Unseed);
}
Long("scope") if op.unwrap_or_default() == OperationName::Seed => {
Long("scope") => {
let val = parser.value()?;
scope = Some(term::args::parse_value("scope", val)?);
}
Long("fetch") if op.unwrap_or_default() == OperationName::Seed => {
Long("fetch") => {
fetch = Some(true);
}
Long("no-fetch") if op.unwrap_or_default() == OperationName::Seed => {
Long("no-fetch") => {
fetch = Some(false);
}
Long("verbose") | Short('v') => verbose = true,
@ -110,13 +94,10 @@ impl Args for Options {
}
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 },
Some(rid) => Operation::Seed {
rid,
fetch: fetch.unwrap_or(true),
scope: scope.unwrap_or(Scope::All),
},
None => Operation::List,
};
@ -130,7 +111,6 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let mut node = radicle::Node::new(profile.socket());
match options.op {
Operation::Unseed { rid } => delete(rid, &mut node, &profile)?,
Operation::Seed { rid, fetch, scope } => {
update(rid, scope, &mut node, &profile)?;

View File

@ -0,0 +1,79 @@
use std::ffi::OsString;
use anyhow::anyhow;
use radicle::{prelude::*, Node};
use crate::terminal::args::{Args, Error, Help};
use crate::{project, terminal as term};
pub const HELP: Help = Help {
name: "unseed",
description: "Remove repository seeding policies",
version: env!("CARGO_PKG_VERSION"),
usage: r#"
Usage
rad unseed <rid> [<option>...]
The `unseed` command removes the seeding policy, if found,
for the given repository.
Options
--help Print help
"#,
};
#[derive(Debug)]
pub struct Options {
rid: RepoId,
}
impl Args for Options {
fn from_args(args: Vec<OsString>) -> anyhow::Result<(Self, Vec<OsString>)> {
use lexopt::prelude::*;
let mut parser = lexopt::Parser::from_args(args);
let mut rid: Option<RepoId> = None;
while let Some(arg) = parser.next()? {
match &arg {
Value(val) => {
rid = Some(term::args::rid(val)?);
}
Long("help") | Short('h') => {
return Err(Error::Help.into());
}
_ => {
return Err(anyhow!(arg.unexpected()));
}
}
}
Ok((
Options {
rid: rid.ok_or(anyhow!(
"A Repository ID must be provided; see `rad unseed --help`"
))?,
},
vec![],
))
}
}
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let profile = ctx.profile()?;
let mut node = radicle::Node::new(profile.socket());
delete(options.rid, &mut node, &profile)?;
Ok(())
}
pub fn delete(rid: RepoId, node: &mut Node, profile: &Profile) -> anyhow::Result<()> {
if project::unseed(rid, node, profile)? {
term::success!("Seeding policy for {} removed", term::format::tertiary(rid));
}
Ok(())
}

View File

@ -268,6 +268,13 @@ fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>>
args.to_vec(),
);
}
"unseed" => {
term::run_command_args::<rad_unseed::Options, _>(
rad_unseed::HELP,
rad_unseed::run,
args.to_vec(),
);
}
"remote" => term::run_command_args::<rad_remote::Options, _>(
rad_remote::HELP,
rad_remote::run,