radicle_cli/commands/
unseed.rs

1use std::ffi::OsString;
2
3use anyhow::anyhow;
4use nonempty::NonEmpty;
5
6use radicle::{prelude::*, Node};
7
8use crate::terminal as term;
9use crate::terminal::args::{Args, Error, Help};
10
11pub const HELP: Help = Help {
12    name: "unseed",
13    description: "Remove repository seeding policies",
14    version: env!("RADICLE_VERSION"),
15    usage: r#"
16Usage
17
18    rad unseed <rid>... [<option>...]
19
20    The `unseed` command removes the seeding policy, if found,
21    for the given repositories.
22
23Options
24
25    --help      Print help
26"#,
27};
28
29#[derive(Debug)]
30pub struct Options {
31    rids: NonEmpty<RepoId>,
32}
33
34impl Args for Options {
35    fn from_args(args: Vec<OsString>) -> anyhow::Result<(Self, Vec<OsString>)> {
36        use lexopt::prelude::*;
37
38        let mut parser = lexopt::Parser::from_args(args);
39        let mut rids: Vec<RepoId> = Vec::new();
40
41        while let Some(arg) = parser.next()? {
42            match &arg {
43                Value(val) => {
44                    let rid = term::args::rid(val)?;
45                    rids.push(rid);
46                }
47                Long("help") | Short('h') => {
48                    return Err(Error::Help.into());
49                }
50                _ => {
51                    return Err(anyhow!(arg.unexpected()));
52                }
53            }
54        }
55
56        Ok((
57            Options {
58                rids: NonEmpty::from_vec(rids).ok_or(anyhow!(
59                    "At least one Repository ID must be provided; see `rad unseed --help`"
60                ))?,
61            },
62            vec![],
63        ))
64    }
65}
66
67pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
68    let profile = ctx.profile()?;
69    let mut node = radicle::Node::new(profile.socket());
70
71    for rid in options.rids {
72        delete(rid, &mut node, &profile)?;
73    }
74
75    Ok(())
76}
77
78pub fn delete(rid: RepoId, node: &mut Node, profile: &Profile) -> anyhow::Result<()> {
79    if profile.unseed(rid, node)? {
80        term::success!("Seeding policy for {} removed", term::format::tertiary(rid));
81    }
82    Ok(())
83}