cli: Refactor `rad issue open`

Lift the issue editing logic into its own function.
This commit is contained in:
Slack Coder 2023-05-05 13:03:21 -05:00 committed by Alexis Sellier
parent 15070c4074
commit 478b61f875
No known key found for this signature in database
1 changed files with 75 additions and 48 deletions

View File

@ -407,27 +407,35 @@ fn list(
Ok(()) Ok(())
} }
fn open<G: Signer>( fn prompt_issue(
issues: &mut Issues, title: &str,
signer: &G, description: &str,
options: &Options, tags: &[Tag],
title: Option<String>, assignees: &[Did],
description: Option<String>, ) -> anyhow::Result<Option<(Metadata, String)>> {
tags: Vec<Tag>, let title = if title.is_empty() {
) -> anyhow::Result<()> { "Enter a title"
} else {
title
};
let description = if description.is_empty() {
"Enter a description..."
} else {
description
};
let meta = Metadata { let meta = Metadata {
title: title.unwrap_or("Enter a title".to_owned()), title: title.to_string(),
tags, tags: tags.to_vec(),
assignees: vec![], assignees: assignees.to_vec(),
}; };
let yaml = serde_yaml::to_string(&meta)?; let yaml = serde_yaml::to_string(&meta)?;
let doc = format!( let doc = format!("{yaml}---\n\n{description}");
"{}---\n\n{}",
yaml, let Some(text) = term::Editor::new().edit(&doc)? else {
description.unwrap_or("Enter a description...".to_owned()) return Ok(None);
); };
if let Some(text) = term::Editor::new().edit(&doc)? {
let mut meta = String::new(); let mut meta = String::new();
let mut frontmatter = false; let mut frontmatter = false;
let mut lines = text.lines(); let mut lines = text.lines();
@ -451,6 +459,26 @@ fn open<G: Signer>(
let meta: Metadata = let meta: Metadata =
serde_yaml::from_str(&meta).context("failed to parse yaml front-matter")?; serde_yaml::from_str(&meta).context("failed to parse yaml front-matter")?;
Ok(Some((meta, description)))
}
fn open<G: Signer>(
issues: &mut Issues,
signer: &G,
options: &Options,
title: Option<String>,
description: Option<String>,
tags: Vec<Tag>,
) -> anyhow::Result<()> {
let Some((meta, description)) = prompt_issue(
&title.unwrap_or_default(),
&description.unwrap_or_default(),
&tags,
&[],
)? else {
return Ok(());
};
let issue = issues.create( let issue = issues.create(
&meta.title, &meta.title,
description.trim(), description.trim(),
@ -465,7 +493,6 @@ fn open<G: Signer>(
if !options.quiet { if !options.quiet {
show_issue(&issue)?; show_issue(&issue)?;
} }
}
Ok(()) Ok(())
} }