From 6fe5dfa3c95f2532f9327e1d0bde7e4f12f57a75 Mon Sep 17 00:00:00 2001 From: Slack Coder Date: Thu, 4 May 2023 16:34:03 -0500 Subject: [PATCH] cli: Refactor `rad issue open` Make code clearer by lifting it into its own method. --- radicle-cli/src/commands/issue.rs | 129 +++++++++++++++++------------- 1 file changed, 75 insertions(+), 54 deletions(-) diff --git a/radicle-cli/src/commands/issue.rs b/radicle-cli/src/commands/issue.rs index a12b6cfa..9130e148 100644 --- a/radicle-cli/src/commands/issue.rs +++ b/radicle-cli/src/commands/issue.rs @@ -7,6 +7,7 @@ use anyhow::{anyhow, Context as _}; use radicle::cob::common::{Reaction, Tag}; use radicle::cob::issue; use radicle::cob::issue::{CloseReason, Issues, State}; +use radicle::crypto::Signer; use radicle::node::Handle; use radicle::prelude::Did; use radicle::profile; @@ -271,61 +272,18 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> { } } Operation::Open { - title, - description, - tags, + ref title, + ref description, + ref tags, } => { - 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::>().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::>() - .as_slice(), - &signer, - )?; - if !options.quiet { - show_issue(&issue)?; - } - } + open( + &mut issues, + &signer, + &options, + title.clone(), + description.clone(), + tags.to_vec(), + )?; } Operation::List { assigned } => { list(&issues, &profile, &assigned)?; @@ -427,6 +385,69 @@ fn list( Ok(()) } +fn open( + issues: &mut Issues, + signer: &G, + options: &Options, + title: Option, + description: Option, + tags: Vec, +) -> 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::>().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::>() + .as_slice(), + signer, + )?; + if !options.quiet { + show_issue(&issue)?; + } + } + + Ok(()) +} + fn show_issue(issue: &issue::Issue) -> anyhow::Result<()> { let tags: Vec = issue.tags().cloned().map(|t| t.into()).collect(); let assignees: Vec = issue