Add `rad-rm` command

Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
xphoniex 2022-11-15 10:48:23 +00:00 committed by Alexis Sellier
parent 12988da089
commit a496eeb84d
No known key found for this signature in database
5 changed files with 118 additions and 2 deletions

View File

@ -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"]

View File

@ -22,6 +22,7 @@ const COMMANDS: &[Help] = &[
rad_ls::HELP,
rad_edit::HELP,
rad_inspect::HELP,
rad_rm::HELP,
HELP,
];

View File

@ -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(())
}

View File

@ -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))
}

View File

@ -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();