Skip to content

Commit f884f58

Browse files
committed
feat: New option date for new post cli command
1 parent f61a932 commit f884f58

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/bin/cobalt/new.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ pub struct NewArgs {
3434
/// Title of the post
3535
pub title: Option<String>,
3636

37+
/// Publish date of the post
38+
///
39+
/// Supported formats:
40+
///
41+
/// "now" - current date, "YYYY-MM-DD HH:MM:SS", "DD Month YYYY HH:MM:SS",
42+
/// "DD Mon YYYY HH:MM:SS", "MM/DD/YYYY HH:MM:SS", "Dow Mon DD HH:MM:SS YYYY"
43+
///
44+
#[arg(long, value_name = "date")]
45+
pub date: Option<String>,
46+
3747
/// New document's parent directory or file (default: `<CWD>/title.ext`)
3848
#[arg(short, long, value_name = "DIR_OR_FILE")]
3949
pub file: Option<path::PathBuf>,
@@ -57,6 +67,7 @@ impl NewArgs {
5767
let config = cobalt::cobalt_model::Config::from_config(config)?;
5868

5969
let title = self.title.as_deref();
70+
let date = self.date.as_deref();
6071

6172
let mut file = env::current_dir().unwrap_or_default();
6273
if let Some(rel_file) = self.file.as_deref() {
@@ -65,7 +76,7 @@ impl NewArgs {
6576

6677
let ext = self.with_ext.as_deref();
6778

68-
create_new_document(&config, title, file, ext, self.edit)
79+
create_new_document(&config, title, date, file, ext, self.edit)
6980
.with_context(|| anyhow::format_err!("Could not create document"))?;
7081

7182
Ok(())
@@ -222,6 +233,7 @@ pub fn create_new_project_for_path(dest: &path::Path) -> Result<()> {
222233
pub fn create_new_document(
223234
config: &cobalt_model::Config,
224235
title: Option<&str>,
236+
date: Option<&str>,
225237
mut file: path::PathBuf,
226238
extension: Option<&str>,
227239
edit: bool,
@@ -297,6 +309,13 @@ pub fn create_new_document(
297309
front.title = Some(liquid::model::KString::from_ref("Untitled"));
298310
}
299311

312+
if let Some(date) = date {
313+
front.published_date = Some(
314+
liquid::model::DateTime::from_str(date)
315+
.ok_or_else(|| anyhow::format_err!("Wrong date format"))?,
316+
);
317+
}
318+
300319
let doc = cobalt_model::Document::new(front.clone(), content);
301320
let mut doc = doc.to_string();
302321
if edit || title.is_none() {

0 commit comments

Comments
 (0)