cli: Refactor `rad issue open`
Lift the issue editing logic into its own function.
This commit is contained in:
parent
15070c4074
commit
478b61f875
|
|
@ -407,6 +407,61 @@ fn list(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn prompt_issue(
|
||||||
|
title: &str,
|
||||||
|
description: &str,
|
||||||
|
tags: &[Tag],
|
||||||
|
assignees: &[Did],
|
||||||
|
) -> anyhow::Result<Option<(Metadata, String)>> {
|
||||||
|
let title = if title.is_empty() {
|
||||||
|
"Enter a title"
|
||||||
|
} else {
|
||||||
|
title
|
||||||
|
};
|
||||||
|
let description = if description.is_empty() {
|
||||||
|
"Enter a description..."
|
||||||
|
} else {
|
||||||
|
description
|
||||||
|
};
|
||||||
|
|
||||||
|
let meta = Metadata {
|
||||||
|
title: title.to_string(),
|
||||||
|
tags: tags.to_vec(),
|
||||||
|
assignees: assignees.to_vec(),
|
||||||
|
};
|
||||||
|
let yaml = serde_yaml::to_string(&meta)?;
|
||||||
|
let doc = format!("{yaml}---\n\n{description}");
|
||||||
|
|
||||||
|
let Some(text) = term::Editor::new().edit(&doc)? else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
|
||||||
|
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")?;
|
||||||
|
|
||||||
|
Ok(Some((meta, description)))
|
||||||
|
}
|
||||||
|
|
||||||
fn open<G: Signer>(
|
fn open<G: Signer>(
|
||||||
issues: &mut Issues,
|
issues: &mut Issues,
|
||||||
signer: &G,
|
signer: &G,
|
||||||
|
|
@ -415,56 +470,28 @@ fn open<G: Signer>(
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
tags: Vec<Tag>,
|
tags: Vec<Tag>,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let meta = Metadata {
|
let Some((meta, description)) = prompt_issue(
|
||||||
title: title.unwrap_or("Enter a title".to_owned()),
|
&title.unwrap_or_default(),
|
||||||
tags,
|
&description.unwrap_or_default(),
|
||||||
assignees: vec![],
|
&tags,
|
||||||
|
&[],
|
||||||
|
)? else {
|
||||||
|
return Ok(());
|
||||||
};
|
};
|
||||||
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 issue = issues.create(
|
||||||
let mut meta = String::new();
|
&meta.title,
|
||||||
let mut frontmatter = false;
|
description.trim(),
|
||||||
let mut lines = text.lines();
|
meta.tags.as_slice(),
|
||||||
|
meta.assignees
|
||||||
while let Some(line) = lines.by_ref().next() {
|
.into_iter()
|
||||||
if line.trim() == "---" {
|
.map(cob::ActorId::from)
|
||||||
if frontmatter {
|
.collect::<Vec<_>>()
|
||||||
break;
|
.as_slice(),
|
||||||
} else {
|
signer,
|
||||||
frontmatter = true;
|
)?;
|
||||||
continue;
|
if !options.quiet {
|
||||||
}
|
show_issue(&issue)?;
|
||||||
}
|
|
||||||
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(())
|
Ok(())
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue