From 19cd53666a0ea39bce9d9380ffd21e810deae6e5 Mon Sep 17 00:00:00 2001 From: Andrew Lilley Brinker Date: Thu, 19 Sep 2024 13:51:34 -0700 Subject: [PATCH] feat: Make `xtask rfd new` include title in RFD file Previously when making a new RFD, the `xtask` command would generate one without a YAML frontmatter, which would cause compilation with `zola` to fail. This fixes that, now including a YAML frontmatter with a `title` field filled with the title the user provided on the CLI. Signed-off-by: Andrew Lilley Brinker --- xtask/src/task/buf.rs | 2 ++ xtask/src/task/rfd.rs | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/xtask/src/task/buf.rs b/xtask/src/task/buf.rs index 67335085..5fa7b573 100644 --- a/xtask/src/task/buf.rs +++ b/xtask/src/task/buf.rs @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 + use crate::workspace; use anyhow::{Context, Result}; use pathbuf::pathbuf; diff --git a/xtask/src/task/rfd.rs b/xtask/src/task/rfd.rs index cbb6f7d6..235e5914 100644 --- a/xtask/src/task/rfd.rs +++ b/xtask/src/task/rfd.rs @@ -3,11 +3,15 @@ //! Tasks to list or create RFDs use crate::{NewRfdArgs, RfdArgs}; -use anyhow::{anyhow, Result}; +use anyhow::{anyhow, Context as _, Result}; use convert_case::{Case, Casing as _}; use glob::{glob, Paths}; use pathbuf::pathbuf; -use std::{fs::File, path::PathBuf}; +use std::{ + fs::File, + io::{BufWriter, Write as _}, + path::PathBuf, +}; /// Run the `rfd` command. pub fn run(args: RfdArgs) -> Result<()> { @@ -38,13 +42,25 @@ fn new(args: NewRfdArgs) -> Result<()> { let title = args.title.to_case(Case::Kebab); let file_name = format!("{:04}-{}.md", id, title); let path = pathbuf![&root, "site", "content", "rfds", &file_name]; - let _ = File::create_new(path)?; + let file = File::create_new(path).context("failed to create new RFD file")?; + + let mut file = BufWriter::new(file); + + writeln!(&mut file, "---") + .and_then(|_| writeln!(&mut file, "title: \"{}\"", args.title)) + .and_then(|_| writeln!(&mut file, "---")) + .context("failed to write to new RFD file")?; + + file.flush() + .context("failed to flush writes to RFD file to disk")?; + log::warn!( "created draft RFD #{}: \"{}\", at '{}'", id, args.title, pathbuf!["docs", "rfds", &file_name].display() ); + Ok(()) }