cli: Refactor `rad issue open`

Make code clearer by lifting it into its own method.
This commit is contained in:
Slack Coder 2023-05-04 16:34:03 -05:00 committed by Alexis Sellier
parent f4dff17085
commit 6fe5dfa3c9
No known key found for this signature in database
1 changed files with 75 additions and 54 deletions

View File

@ -7,6 +7,7 @@ use anyhow::{anyhow, Context as _};
use radicle::cob::common::{Reaction, Tag}; use radicle::cob::common::{Reaction, Tag};
use radicle::cob::issue; use radicle::cob::issue;
use radicle::cob::issue::{CloseReason, Issues, State}; use radicle::cob::issue::{CloseReason, Issues, State};
use radicle::crypto::Signer;
use radicle::node::Handle; use radicle::node::Handle;
use radicle::prelude::Did; use radicle::prelude::Did;
use radicle::profile; use radicle::profile;
@ -271,61 +272,18 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
} }
} }
Operation::Open { Operation::Open {
title, ref title,
description, ref description,
tags, ref tags,
} => { } => {
let meta = Metadata { open(
title: title.unwrap_or("Enter a title".to_owned()), &mut issues,
tags, &signer,
assignees: vec![], &options,
}; title.clone(),
let yaml = serde_yaml::to_string(&meta)?; description.clone(),
let doc = format!( tags.to_vec(),
"{}---\n\n{}", )?;
yaml,
description.unwrap_or("Enter a description...".to_owned())
);
if let Some(text) = term::Editor::new().edit(&doc)? {
let mut meta = String::new();
let mut frontmatter = false;
let mut lines = text.lines();
while let Some(line) = lines.by_ref().next() {
if line.trim() == "---" {
if frontmatter {
break;
} else {
frontmatter = true;
continue;
}
}
if frontmatter {
meta.push_str(line);
meta.push('\n');
}
}
let description: String = lines.collect::<Vec<&str>>().join("\n");
let meta: Metadata =
serde_yaml::from_str(&meta).context("failed to parse yaml front-matter")?;
let issue = issues.create(
&meta.title,
description.trim(),
meta.tags.as_slice(),
meta.assignees
.into_iter()
.map(cob::ActorId::from)
.collect::<Vec<_>>()
.as_slice(),
&signer,
)?;
if !options.quiet {
show_issue(&issue)?;
}
}
} }
Operation::List { assigned } => { Operation::List { assigned } => {
list(&issues, &profile, &assigned)?; list(&issues, &profile, &assigned)?;
@ -427,6 +385,69 @@ fn list(
Ok(()) Ok(())
} }
fn open<G: Signer>(
issues: &mut Issues,
signer: &G,
options: &Options,
title: Option<String>,
description: Option<String>,
tags: Vec<Tag>,
) -> anyhow::Result<()> {
let meta = Metadata {
title: title.unwrap_or("Enter a title".to_owned()),
tags,
assignees: vec![],
};
let yaml = serde_yaml::to_string(&meta)?;
let doc = format!(
"{}---\n\n{}",
yaml,
description.unwrap_or("Enter a description...".to_owned())
);
if let Some(text) = term::Editor::new().edit(&doc)? {
let mut meta = String::new();
let mut frontmatter = false;
let mut lines = text.lines();
while let Some(line) = lines.by_ref().next() {
if line.trim() == "---" {
if frontmatter {
break;
} else {
frontmatter = true;
continue;
}
}
if frontmatter {
meta.push_str(line);
meta.push('\n');
}
}
let description: String = lines.collect::<Vec<&str>>().join("\n");
let meta: Metadata =
serde_yaml::from_str(&meta).context("failed to parse yaml front-matter")?;
let issue = issues.create(
&meta.title,
description.trim(),
meta.tags.as_slice(),
meta.assignees
.into_iter()
.map(cob::ActorId::from)
.collect::<Vec<_>>()
.as_slice(),
signer,
)?;
if !options.quiet {
show_issue(&issue)?;
}
}
Ok(())
}
fn show_issue(issue: &issue::Issue) -> anyhow::Result<()> { fn show_issue(issue: &issue::Issue) -> anyhow::Result<()> {
let tags: Vec<String> = issue.tags().cloned().map(|t| t.into()).collect(); let tags: Vec<String> = issue.tags().cloned().map(|t| t.into()).collect();
let assignees: Vec<String> = issue let assignees: Vec<String> = issue