Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: New option date for new post cli command #1152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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