cli/clean: Use clap
This commit is contained in:
parent
753b7aef93
commit
6fb1ebec45
|
|
@ -1,86 +1,24 @@
|
||||||
use std::ffi::OsString;
|
mod args;
|
||||||
|
|
||||||
use anyhow::anyhow;
|
|
||||||
|
|
||||||
use radicle::identity::RepoId;
|
|
||||||
use radicle::storage;
|
use radicle::storage;
|
||||||
use radicle::storage::WriteStorage;
|
use radicle::storage::WriteStorage;
|
||||||
|
|
||||||
use crate::terminal as term;
|
use crate::terminal as term;
|
||||||
use crate::terminal::args::{Args, Error, Help};
|
|
||||||
|
|
||||||
pub const HELP: Help = Help {
|
pub use args::Args;
|
||||||
name: "clean",
|
pub(crate) use args::ABOUT;
|
||||||
description: "Remove all remotes from a repository",
|
|
||||||
version: env!("RADICLE_VERSION"),
|
|
||||||
usage: r#"
|
|
||||||
Usage
|
|
||||||
|
|
||||||
rad clean <rid> [<option>...]
|
pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
|
||||||
|
|
||||||
Removes all remotes from a repository, as long as they are not the
|
|
||||||
local operator or a delegate of the repository.
|
|
||||||
|
|
||||||
Note that remotes will still be fetched as long as they are
|
|
||||||
followed and/or the follow scope is "all".
|
|
||||||
|
|
||||||
Options
|
|
||||||
|
|
||||||
--no-confirm Do not ask for confirmation before removal (default: false)
|
|
||||||
--help Print help
|
|
||||||
"#,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Options {
|
|
||||||
rid: RepoId,
|
|
||||||
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<RepoId> = None;
|
|
||||||
let mut confirm = true;
|
|
||||||
|
|
||||||
while let Some(arg) = parser.next()? {
|
|
||||||
match arg {
|
|
||||||
Long("no-confirm") => {
|
|
||||||
confirm = false;
|
|
||||||
}
|
|
||||||
Long("help") | Short('h') => {
|
|
||||||
return Err(Error::Help.into());
|
|
||||||
}
|
|
||||||
Value(val) if id.is_none() => {
|
|
||||||
id = Some(term::args::rid(&val)?);
|
|
||||||
}
|
|
||||||
_ => anyhow::bail!(arg.unexpected()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok((
|
|
||||||
Options {
|
|
||||||
rid: id
|
|
||||||
.ok_or_else(|| anyhow!("an RID must be provided; see `rad clean --help`"))?,
|
|
||||||
confirm,
|
|
||||||
},
|
|
||||||
vec![],
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
|
|
||||||
let profile = ctx.profile()?;
|
let profile = ctx.profile()?;
|
||||||
let storage = &profile.storage;
|
let storage = &profile.storage;
|
||||||
let rid = options.rid;
|
let rid = args.repo;
|
||||||
let path = storage::git::paths::repository(storage, &rid);
|
let path = storage::git::paths::repository(storage, &rid);
|
||||||
|
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
anyhow::bail!("repository {rid} was not found");
|
anyhow::bail!("repository {rid} was not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
if !options.confirm || term::confirm(format!("Clean {rid}?")) {
|
if args.no_confirm || term::confirm(format!("Clean {rid}?")) {
|
||||||
let cleaned = storage.clean(rid)?;
|
let cleaned = storage.clean(rid)?;
|
||||||
for remote in cleaned {
|
for remote in cleaned {
|
||||||
term::info!("Removed {remote}");
|
term::info!("Removed {remote}");
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
use clap::Parser;
|
||||||
|
|
||||||
|
use radicle::prelude::RepoId;
|
||||||
|
|
||||||
|
pub const ABOUT: &str = "Remove all remotes from a repository";
|
||||||
|
|
||||||
|
const LONG_ABOUT: &str = r#"
|
||||||
|
Removes all remotes from a repository, as long as they are not the
|
||||||
|
local operator or a delegate of the repository.
|
||||||
|
|
||||||
|
Note that remotes will still be fetched as long as they are
|
||||||
|
followed and/or the follow scope is "all".
|
||||||
|
"#;
|
||||||
|
|
||||||
|
#[derive(Debug, Parser)]
|
||||||
|
#[command(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)]
|
||||||
|
pub struct Args {
|
||||||
|
/// Operate on the given repository
|
||||||
|
#[arg(value_name = "RID")]
|
||||||
|
pub(super) repo: RepoId,
|
||||||
|
|
||||||
|
/// Do not ask for confirmation before removal
|
||||||
|
#[arg(long)]
|
||||||
|
pub(super) no_confirm: bool,
|
||||||
|
}
|
||||||
|
|
@ -59,7 +59,10 @@ const COMMANDS: &[CommandItem] = &[
|
||||||
name: "path",
|
name: "path",
|
||||||
about: crate::commands::path::ABOUT,
|
about: crate::commands::path::ABOUT,
|
||||||
},
|
},
|
||||||
CommandItem::Lexopt(crate::commands::clean::HELP),
|
CommandItem::Clap {
|
||||||
|
name: "clean",
|
||||||
|
about: crate::commands::clean::ABOUT,
|
||||||
|
},
|
||||||
CommandItem::Lexopt(crate::commands::rad_self::HELP),
|
CommandItem::Lexopt(crate::commands::rad_self::HELP),
|
||||||
CommandItem::Lexopt(crate::commands::seed::HELP),
|
CommandItem::Lexopt(crate::commands::seed::HELP),
|
||||||
CommandItem::Lexopt(crate::commands::follow::HELP),
|
CommandItem::Lexopt(crate::commands::follow::HELP),
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,7 @@ struct CliArgs {
|
||||||
|
|
||||||
#[derive(Subcommand, Debug)]
|
#[derive(Subcommand, Debug)]
|
||||||
enum Commands {
|
enum Commands {
|
||||||
|
Clean(clean::Args),
|
||||||
Issue(issue::Args),
|
Issue(issue::Args),
|
||||||
Path(path::Args),
|
Path(path::Args),
|
||||||
Stats(stats::Args),
|
Stats(stats::Args),
|
||||||
|
|
@ -252,7 +253,9 @@ pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyho
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
"clean" => {
|
"clean" => {
|
||||||
term::run_command_args::<clean::Options, _>(clean::HELP, clean::run, args.to_vec());
|
if let Some(Commands::Clean(args)) = CliArgs::parse().command {
|
||||||
|
term::run_command_fn(clean::run, args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
"self" => {
|
"self" => {
|
||||||
term::run_command_args::<rad_self::Options, _>(
|
term::run_command_args::<rad_self::Options, _>(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue