Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
Add `rad-rm` command
xphoniex committed 3 years ago
commit a496eeb84da629a6710080963bdfebbb8c65d6ef
parent 12988da089bfd916e7eb6e11563a970a13825603
5 files changed +118 -2
modified radicle-cli/src/commands.rs
@@ -14,6 +14,8 @@ pub mod rad_init;
pub mod rad_inspect;
#[path = "commands/ls.rs"]
pub mod rad_ls;
+
#[path = "commands/rm.rs"]
+
pub mod rad_rm;
#[path = "commands/self.rs"]
pub mod rad_self;
#[path = "commands/track.rs"]
modified radicle-cli/src/commands/help.rs
@@ -22,6 +22,7 @@ const COMMANDS: &[Help] = &[
    rad_ls::HELP,
    rad_edit::HELP,
    rad_inspect::HELP,
+
    rad_rm::HELP,
    HELP,
];

added radicle-cli/src/commands/rm.rs
@@ -0,0 +1,101 @@
+
use std::ffi::OsString;
+
use std::fs;
+
use std::str::FromStr;
+

+
use anyhow::anyhow;
+

+
use radicle::identity::project::Id;
+
use radicle::storage::ReadStorage;
+

+
use crate::commands::rad_untrack;
+
use crate::terminal as term;
+
use crate::terminal::args::{Args, Error, Help};
+

+
pub const HELP: Help = Help {
+
    name: "rm",
+
    description: "Remove radicle projects",
+
    version: env!("CARGO_PKG_VERSION"),
+
    usage: r#"
+
Usage
+

+
    rad rm <id> [<option>...]
+

+
    Removes a project from storage.
+

+
Options
+

+
    --no-confirm        Do not ask for confirmation before removal
+
                        (default: false)
+
    --help              Print help
+
"#,
+
};
+

+
pub struct Options {
+
    id: Id,
+
    confirm: bool,
+
}
+

+
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 id: Option<Id> = None;
+
        let mut confirm = true;
+

+
        while let Some(arg) = parser.next()? {
+
            match arg {
+
                Long("no-confirm") => {
+
                    confirm = false;
+
                }
+
                Long("help") => {
+
                    return Err(Error::Help.into());
+
                }
+
                Value(val) if id.is_none() => {
+
                    let val = val.to_string_lossy();
+

+
                    if let Ok(val) = Id::from_str(&val) {
+
                        id = Some(val);
+
                    } else {
+
                        return Err(anyhow!("invalid ID '{}'", val));
+
                    }
+
                }
+
                _ => return Err(anyhow::anyhow!(arg.unexpected())),
+
            }
+
        }
+

+
        Ok((
+
            Options {
+
                id: id.ok_or_else(|| anyhow!("an `id` must be provided; see `rad rm --help`"))?,
+
                confirm,
+
            },
+
            vec![],
+
        ))
+
    }
+
}
+

+
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
+
    let profile = ctx.profile()?;
+
    let storage = &profile.storage;
+
    let signer = term::signer(&profile)?;
+
    let id = options.id;
+

+
    if let Ok(Some(_)) = storage.get(signer.public_key(), id.to_owned()) {
+
        let namespace = profile.paths().storage().join(&id.to_human());
+

+
        if !options.confirm
+
            || term::confirm(format!(
+
                "Are you sure you would like to delete {}?",
+
                term::format::dim(id.to_human())
+
            ))
+
        {
+
            rad_untrack::untrack(id.to_owned(), &profile)?;
+
            fs::remove_dir_all(namespace)?;
+
            term::success!("Successfully removed project {}", &id);
+
        }
+
    } else {
+
        anyhow::bail!("project {} does not exist", &id)
+
    }
+

+
    Ok(())
+
}
modified radicle-cli/src/commands/untrack.rs
@@ -69,9 +69,8 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
    let profile = ctx.profile()?;
    let storage = &profile.storage;
    let Doc { payload, .. } = storage.repository(id)?.project_of(profile.id())?;
-
    let node = radicle::node::connect(&profile.node())?;

-
    if node.untrack(&id)? {
+
    if untrack(id, &profile)? {
        term::success!(
            "Tracking relationships for {} ({}) removed",
            term::format::highlight(payload.name),
@@ -87,3 +86,8 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {

    Ok(())
}
+

+
pub fn untrack(id: Id, profile: &Profile) -> anyhow::Result<bool> {
+
    let node = radicle::node::connect(profile.node())?;
+
    node.untrack(&id).map_err(|e| anyhow!(e))
+
}
modified radicle-cli/src/main.rs
@@ -182,6 +182,14 @@ fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>>
                args.to_vec(),
            );
        }
+
        "rm" => {
+
            term::run_command_args::<rad_rm::Options, _>(
+
                rad_rm::HELP,
+
                "Remove",
+
                rad_rm::run,
+
                args.to_vec(),
+
            );
+
        }
        _ => {
            let exe = format!("{}-{}", NAME, exe);
            let status = process::Command::new(exe.clone()).args(args).status();