cli: Polish `assign` and `unassign` commands

This commit is contained in:
Alexis Sellier 2023-03-12 20:26:24 +01:00 committed by Alexis Sellier
parent 49a6dacd75
commit f7aac06a8f
No known key found for this signature in database
5 changed files with 70 additions and 60 deletions

1
Cargo.lock generated
View File

@ -1825,6 +1825,7 @@ dependencies = [
"json-color", "json-color",
"lexopt", "lexopt",
"log", "log",
"nonempty 0.8.1",
"pretty_assertions", "pretty_assertions",
"radicle", "radicle",
"radicle-cli-test", "radicle-cli-test",

View File

@ -16,6 +16,7 @@ chrono = { version = "0.4", default-features = false, features = ["clock", "std"
json-color = { version = "0.7" } json-color = { version = "0.7" }
lexopt = { version = "0.2" } lexopt = { version = "0.2" }
log = { version = "0.4", features = ["std"] } log = { version = "0.4", features = ["std"] }
nonempty = { version = "0.8" }
serde = { version = "1.0" } serde = { version = "1.0" }
serde_json = { version = "1" } serde_json = { version = "1" }
serde_yaml = { version = "0.8" } serde_yaml = { version = "0.8" }

View File

@ -28,7 +28,7 @@ others to work on. This is to ensure work is not duplicated.
Let's assign ourselves to this one. Let's assign ourselves to this one.
``` ```
$ rad assign 2e8c1bf3fe0532a314778357c886608a966a34bd did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi $ rad assign 2e8c1bf3fe0532a314778357c886608a966a34bd --to did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
``` ```
It will now show in the list of issues assigned to us. It will now show in the list of issues assigned to us.
@ -41,7 +41,7 @@ $ rad issue list --assigned
Note: this can always be undone with the `unassign` subcommand. Note: this can always be undone with the `unassign` subcommand.
``` ```
$ rad unassign 2e8c1bf3fe0532a314778357c886608a966a34bd did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi $ rad unassign 2e8c1bf3fe0532a314778357c886608a966a34bd --from did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
``` ```
Great, now we have communicated to the world about our car's defect. Great, now we have communicated to the world about our car's defect.

View File

@ -2,61 +2,65 @@ use std::ffi::OsString;
use std::str::FromStr; use std::str::FromStr;
use anyhow::anyhow; use anyhow::anyhow;
use nonempty::NonEmpty;
use radicle::prelude::Did; use radicle::prelude::Did;
use crate::terminal as term; use crate::terminal as term;
use crate::terminal::args; use crate::terminal::args::{Args, Error, Help};
use radicle::cob; use radicle::cob;
use radicle::cob::issue; use radicle::cob::issue;
use radicle::storage::WriteStorage; use radicle::storage::WriteStorage;
pub const HELP: args::Help = args::Help { pub const HELP: Help = Help {
name: "assign", name: "assign",
description: "assign an issue", description: "Assign an issue",
version: env!("CARGO_PKG_VERSION"), version: env!("CARGO_PKG_VERSION"),
usage: r#" usage: r#"
Usage Usage
rad assign <issue> <did> rad assign <issue> --to <did>
To assign multiple users to an issue, you may repeat
the `--to` option.
Options Options
--help Print help --to <did> Assignee to add to the issue
--help Print help
"#, "#,
}; };
#[derive(Debug)] #[derive(Debug)]
pub struct Options { pub struct Options {
pub id: issue::IssueId, pub id: issue::IssueId,
pub peer: Did, pub to: NonEmpty<Did>,
} }
impl args::Args for Options { impl Args for Options {
fn from_args(args: Vec<OsString>) -> anyhow::Result<(Self, Vec<OsString>)> { fn from_args(args: Vec<OsString>) -> anyhow::Result<(Self, Vec<OsString>)> {
use lexopt::prelude::*; use lexopt::prelude::*;
let mut parser = lexopt::Parser::from_args(args); let mut parser = lexopt::Parser::from_args(args);
let mut id: Option<issue::IssueId> = None; let mut id: Option<issue::IssueId> = None;
let mut peer: Option<Did> = None; let mut to: Vec<Did> = Vec::new();
while let Some(arg) = parser.next()? { while let Some(arg) = parser.next()? {
match arg { match arg {
Long("help") => { Long("help") => {
return Err(args::Error::Help.into()); return Err(Error::Help.into());
} }
Value(ref val) => { Long("to") => {
if id.is_none() { let val = parser.value()?;
let val = val.to_string_lossy(); let did = term::args::did(&val)?;
let Ok(val) = issue::IssueId::from_str(&val) else {
return Err(anyhow!("invalid issue ID '{}'", val));
};
id = Some(val); to.push(did);
} else if peer.is_none() { }
peer = Some(term::args::did(val)?); Value(ref val) if id.is_none() => {
} else { let val = val.to_string_lossy();
return Err(anyhow!(arg.unexpected())); let Ok(val) = issue::IssueId::from_str(&val) else {
} return Err(anyhow!("invalid Issue ID '{}'", val));
};
id = Some(val);
} }
_ => { _ => {
return Err(anyhow!(arg.unexpected())); return Err(anyhow!(arg.unexpected()));
@ -66,8 +70,9 @@ impl args::Args for Options {
Ok(( Ok((
Options { Options {
id: id.unwrap(), id: id.ok_or_else(|| anyhow!("an issue must be specified"))?,
peer: peer.unwrap(), to: NonEmpty::from_vec(to)
.ok_or_else(|| anyhow!("an assignee must be specified"))?,
}, },
vec![], vec![],
)) ))
@ -76,17 +81,16 @@ impl args::Args for Options {
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> { pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let profile = ctx.profile()?; let profile = ctx.profile()?;
let signer = term::signer(&profile)?;
let storage = &profile.storage;
let (_, id) = radicle::rad::cwd()?; let (_, id) = radicle::rad::cwd()?;
let repo = storage.repository_mut(id)?; let repo = profile.storage.repository_mut(id)?;
let mut issues = issue::Issues::open(&repo)?; let mut issues = issue::Issues::open(&repo)?;
let mut issue = issues.get_mut(&options.id).map_err(|e| match e {
let mut issue = issues.get_mut(&options.id).map_err(|err| match err { cob::store::Error::NotFound(_, _) => anyhow!("issue {} not found", options.id),
cob::store::Error::NotFound(_, _) => anyhow!("issue not found '{}'", options.id), _ => e.into(),
_ => err.into(),
})?; })?;
issue.assign(vec![*options.peer], &signer)?; let signer = term::signer(&profile)?;
issue.assign(options.to.into_iter().map(Did::into), &signer)?;
Ok(()) Ok(())
} }

View File

@ -2,6 +2,7 @@ use std::ffi::OsString;
use std::str::FromStr; use std::str::FromStr;
use anyhow::anyhow; use anyhow::anyhow;
use nonempty::NonEmpty;
use radicle::prelude::Did; use radicle::prelude::Did;
use crate::terminal as term; use crate::terminal as term;
@ -12,23 +13,27 @@ use radicle::storage::WriteStorage;
pub const HELP: Help = Help { pub const HELP: Help = Help {
name: "unassign", name: "unassign",
description: "unassign an issue", description: "Unassign an issue",
version: env!("CARGO_PKG_VERSION"), version: env!("CARGO_PKG_VERSION"),
usage: r#" usage: r#"
Usage Usage
rad unassign <issue> <peer> rad unassign <issue> --from <did>
To unassign multiple users from an issue, you may repeat
the `--from` option.
Options Options
--help Print help --from <did> Assignee to remove from the issue
--help Print help
"#, "#,
}; };
#[derive(Debug)] #[derive(Debug)]
pub struct Options { pub struct Options {
pub id: issue::IssueId, pub id: issue::IssueId,
pub peer: Did, pub from: NonEmpty<Did>,
} }
impl Args for Options { impl Args for Options {
@ -37,26 +42,25 @@ impl Args for Options {
let mut parser = lexopt::Parser::from_args(args); let mut parser = lexopt::Parser::from_args(args);
let mut id: Option<issue::IssueId> = None; let mut id: Option<issue::IssueId> = None;
let mut peer: Option<Did> = None; let mut from: Vec<Did> = Vec::new();
while let Some(arg) = parser.next()? { while let Some(arg) = parser.next()? {
match arg { match arg {
Long("help") => { Long("help") => {
return Err(Error::Help.into()); return Err(Error::Help.into());
} }
Value(ref val) => { Long("from") => {
if id.is_none() { let val = parser.value()?;
let val = val.to_string_lossy(); let did = term::args::did(&val)?;
let Ok(val) = issue::IssueId::from_str(&val) else {
return Err(anyhow!("invalid issue ID '{}'", val));
};
id = Some(val); from.push(did);
} else if peer.is_none() { }
peer = Some(term::args::did(val)?); Value(ref val) if id.is_none() => {
} else { let val = val.to_string_lossy();
return Err(anyhow!(arg.unexpected())); let Ok(val) = issue::IssueId::from_str(&val) else {
} return Err(anyhow!("invalid Issue ID '{}'", val));
};
id = Some(val);
} }
_ => { _ => {
return Err(anyhow!(arg.unexpected())); return Err(anyhow!(arg.unexpected()));
@ -66,8 +70,9 @@ impl Args for Options {
Ok(( Ok((
Options { Options {
id: id.unwrap(), id: id.ok_or_else(|| anyhow!("an issue must be specified"))?,
peer: peer.unwrap(), from: NonEmpty::from_vec(from)
.ok_or_else(|| anyhow!("an assignee must be specified"))?,
}, },
vec![], vec![],
)) ))
@ -76,17 +81,16 @@ impl Args for Options {
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> { pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
let profile = ctx.profile()?; let profile = ctx.profile()?;
let signer = term::signer(&profile)?;
let storage = &profile.storage;
let (_, id) = radicle::rad::cwd()?; let (_, id) = radicle::rad::cwd()?;
let repo = storage.repository_mut(id)?; let repo = profile.storage.repository_mut(id)?;
let mut issues = issue::Issues::open(&repo)?; let mut issues = issue::Issues::open(&repo)?;
let mut issue = issues.get_mut(&options.id).map_err(|e| match e {
let mut issue = issues.get_mut(&options.id).map_err(|err| match err { cob::store::Error::NotFound(_, _) => anyhow!("issue {} not found", options.id),
cob::store::Error::NotFound(_, _) => anyhow!("issue '{}' not found", options.id), _ => e.into(),
_ => err.into(),
})?; })?;
issue.unassign(vec![*options.peer], &signer)?; let signer = term::signer(&profile)?;
issue.unassign(options.from.into_iter().map(Did::into), &signer)?;
Ok(()) Ok(())
} }