use std::fs;
pub struct NewPost;
impl NewPost {
fn template(title: &str) -> String {
format!(
r#"+++
title: {}
date: {}
+++
"#,
title,
chrono::Local::now().format("%Y-%m-%d")
)
}
pub fn run(&self, title: &str) -> crate::BlogResult<()> {
let template = NewPost::template(title);
fs::write(format!("./posts/{title}.md"), template)
.map_err(|e| crate::BlogError::io(e, "Unable to create new post ensure the /posts directory exits and you are in your root".to_string()))?;
println!("Created new post {title}.md");
Ok(())
}
}