Skip to content

Commit

Permalink
feat: New option date for new post cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Oct 3, 2023
1 parent b3af49a commit dc7d202
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/bin/cobalt/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ pub struct NewArgs {
/// Title of the post
pub title: Option<String>,

/// Publish date of the post
///
/// Supported formats:
///
/// "now" - current date, "YYYY-MM-DD HH:MM:SS", "DD Month YYYY HH:MM:SS",
/// "DD Mon YYYY HH:MM:SS", "MM/DD/YYYY HH:MM:SS", "Dow Mon DD HH:MM:SS YYYY"
///
#[arg(long, value_name = "date")]
pub date: Option<String>,

/// New document's parent directory or file (default: `<CWD>/title.ext`)
#[arg(short, long, value_name = "DIR_OR_FILE")]
pub file: Option<path::PathBuf>,
Expand All @@ -57,6 +67,7 @@ impl NewArgs {
let config = cobalt::cobalt_model::Config::from_config(config)?;

let title = self.title.as_deref();
let date = self.date.as_deref();

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

let ext = self.with_ext.as_deref();

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

Ok(())
Expand Down Expand Up @@ -222,6 +233,7 @@ pub fn create_new_project_for_path(dest: &path::Path) -> Result<()> {
pub fn create_new_document(
config: &cobalt_model::Config,
title: Option<&str>,
date: Option<&str>,
mut file: path::PathBuf,
extension: Option<&str>,
edit: bool,
Expand Down Expand Up @@ -297,6 +309,13 @@ pub fn create_new_document(
front.title = Some(liquid::model::KString::from_ref("Untitled"));
}

if let Some(date) = date {
front.published_date = Some(
liquid::model::DateTime::from_str(date)
.ok_or_else(|| anyhow::format_err!("Wrong date format"))?,
);
}

let doc = cobalt_model::Document::new(front.clone(), content);
let mut doc = doc.to_string();
if edit || title.is_none() {
Expand Down

0 comments on commit dc7d202

Please sign in to comment.