cli: Fix `rad seed --delete` command

There was a bug due to `--delete` not being treated properly as a
sub-command.
This commit is contained in:
cloudhead 2023-12-12 10:56:23 +01:00
parent bfdf82a69a
commit 74a0d7a4d2
No known key found for this signature in database
1 changed files with 46 additions and 30 deletions

View File

@ -39,12 +39,23 @@ Options
"#, "#,
}; };
#[derive(Debug)]
pub enum Operation {
Seed { fetch: bool, scope: Scope },
Unseed,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
pub enum OperationName {
#[default]
Seed,
Unseed,
}
#[derive(Debug)] #[derive(Debug)]
pub struct Options { pub struct Options {
pub rid: Id, pub rid: Id,
pub scope: Scope, pub op: Operation,
pub delete: bool,
pub fetch: bool,
pub verbose: bool, pub verbose: bool,
} }
@ -55,8 +66,8 @@ impl Args for Options {
let mut parser = lexopt::Parser::from_args(args); let mut parser = lexopt::Parser::from_args(args);
let mut rid: Option<Id> = None; let mut rid: Option<Id> = None;
let mut scope: Option<Scope> = None; let mut scope: Option<Scope> = None;
let mut fetch = true; let mut fetch: Option<bool> = None;
let mut delete = false; let mut op: Option<OperationName> = None;
let mut verbose = false; let mut verbose = false;
while let Some(arg) = parser.next()? { while let Some(arg) = parser.next()? {
@ -64,13 +75,19 @@ impl Args for Options {
Value(val) => { Value(val) => {
rid = Some(term::args::rid(val)?); rid = Some(term::args::rid(val)?);
} }
Long("scope") if scope.is_none() => { Long("delete") | Short('d') if op.is_none() => {
op = Some(OperationName::Unseed);
}
Long("scope") if op.unwrap_or_default() == OperationName::Seed => {
let val = parser.value()?; let val = parser.value()?;
scope = Some(term::args::parse_value("scope", val)?); scope = Some(term::args::parse_value("scope", val)?);
} }
Long("delete") | Short('d') => delete = true, Long("fetch") if op.unwrap_or_default() == OperationName::Seed => {
Long("fetch") => fetch = true, fetch = Some(true);
Long("no-fetch") => fetch = false, }
Long("no-fetch") if op.unwrap_or_default() == OperationName::Seed => {
fetch = Some(false);
}
Long("verbose") | Short('v') => verbose = true, Long("verbose") | Short('v') => verbose = true,
Long("help") | Short('h') => { Long("help") | Short('h') => {
return Err(Error::Help.into()); return Err(Error::Help.into());
@ -81,19 +98,18 @@ impl Args for Options {
} }
} }
if scope.is_some() && delete { let op = match op.unwrap_or_default() {
anyhow::bail!("`--scope` may not be used with `--delete` or `-d`"); OperationName::Seed => Operation::Seed {
} fetch: fetch.unwrap_or(true),
if fetch && delete { scope: scope.unwrap_or(Scope::All),
anyhow::bail!("`--fetch` may not be used with `--delete` or `-d`"); },
} OperationName::Unseed => Operation::Unseed,
};
Ok(( Ok((
Options { Options {
rid: rid.ok_or_else(|| anyhow!("a Repository ID must be specified"))?, rid: rid.ok_or_else(|| anyhow!("a Repository ID must be specified"))?,
scope: scope.unwrap_or(Scope::All), op,
delete,
fetch,
verbose, verbose,
}, },
vec![], vec![],
@ -105,20 +121,20 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let profile = ctx.profile()?; let profile = ctx.profile()?;
let mut node = radicle::Node::new(profile.socket()); let mut node = radicle::Node::new(profile.socket());
let rid = options.rid; let rid = options.rid;
let scope = options.scope;
if options.delete { match options.op {
delete(rid, &mut node, &profile)?; Operation::Unseed => delete(rid, &mut node, &profile)?,
} else { Operation::Seed { fetch, scope } => {
update(rid, scope, &mut node, &profile)?; update(rid, scope, &mut node, &profile)?;
if options.fetch && node.is_running() { if fetch && node.is_running() {
sync::fetch( sync::fetch(
rid, rid,
sync::RepoSync::default(), sync::RepoSync::default(),
time::Duration::from_secs(6), time::Duration::from_secs(6),
&mut node, &mut node,
)?; )?;
}
} }
} }