From 02c53afea1ec6c6ae5060e01f908497a01f03981 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 15 Oct 2020 09:41:20 +0300 Subject: feat: create new draft, publish it --- src/main.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 4e0a887..cb24474 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,23 @@ enum JT { #[structopt(help = "Directory that may or may not be a journal")] dirname: PathBuf, }, + New { + #[structopt(long, short, help = "Use DIRNAME as the location of the journal")] + dirname: PathBuf, + #[structopt( + long, + short, + help = "Invoke EDITOR for user to edit draft", + default_value = "/usr/bin/editor" + )] + editor: String, + #[structopt(help = "Title of new draft")] + title: String, + }, + Finish { + #[structopt(long, short, help = "Use DIRNAME as the location of the journal")] + dirname: PathBuf, + }, } #[derive(Debug, Error)] @@ -36,6 +53,12 @@ fn main() -> Result<()> { description, } => init(&dirname, &journalname, &description)?, JT::IsJournal { dirname } => is_journal(&dirname)?, + JT::New { + title, + dirname, + editor, + } => new_draft(&title, &dirname, &editor)?, + JT::Finish { dirname } => finish_draft(&dirname)?, } Ok(()) } @@ -52,3 +75,27 @@ fn is_journal(dirname: &Path) -> anyhow::Result<()> { } Ok(()) } + +fn new_draft(title: &str, dirname: &Path, editor: &str) -> anyhow::Result<()> { + let drafts = dirname.join("drafts"); + if !drafts.exists() { + std::fs::create_dir(&drafts)?; + } + let draft_filename = drafts.join("0.md"); + std::fs::write(draft_filename, title)?; + Ok(()) +} + +fn finish_draft(dirname: &Path) -> anyhow::Result<()> { + let drafts = dirname.join("drafts"); + let draft = drafts.join("0.md"); + + let entries = dirname.join("entries"); + if !entries.exists() { + std::fs::create_dir(&entries)?; + } + let entry = entries.join("0.md"); + + std::fs::rename(draft, entry)?; + Ok(()) +} -- cgit v1.2.1