From 86050e18b21181a4006fd1a5b872c792e7900683 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 20 Jul 2021 10:29:40 +0300 Subject: feat: add command to remove a draft Sponsored-by: author --- jt.md | 30 ++++++++++++++++++++++++++++++ src/bin/jt2.rs | 1 + src/cmd.rs | 15 +++++++++++++++ src/error.rs | 4 ++++ src/journal.rs | 6 ++++++ src/opt.rs | 3 +++ 6 files changed, 59 insertions(+) diff --git a/jt.md b/jt.md index f2729ff..9c561da 100644 --- a/jt.md +++ b/jt.md @@ -231,6 +231,36 @@ then there are no uncommitted changes in jrnl +## Remove a draft + +Verify that we can remove a draft, and then create a new one. + +~~~scenario +given an installed jt + +when I run jt2 --dirname jrnl init default "My test journal" +then command is successful +and there are no drafts in jrnl +and there are no journal entries in jrnl + +when I run jt2 --editor=none --dirname=jrnl new "Hulabaloo" +then command is successful +and there is one draft in jrnl +and draft 0 in jrnl contains "Hulabaloo" +and draft 0 in jrnl contains "!meta date=" + +when I run jt2 --dirname=jrnl remove 0 +then command is successful +and there are no drafts in jrnl +and there are no journal entries in jrnl + +when I run jt2 --editor=none --dirname=jrnl new "Abracadabra" +then command is successful +and there is one draft in jrnl +and draft 0 in jrnl contains "Abracadabra" +and draft 0 in jrnl contains "!meta date=" +~~~ + ## Override template for new journal entries Verify that we can have a custom template for new journal entries. diff --git a/src/bin/jt2.rs b/src/bin/jt2.rs index 1574f2e..a5760d8 100644 --- a/src/bin/jt2.rs +++ b/src/bin/jt2.rs @@ -22,6 +22,7 @@ fn do_work() -> anyhow::Result<()> { SubCommand::NewTopic(x) => x.run(&config)?, SubCommand::List(x) => x.run(&config)?, SubCommand::Edit(x) => x.run(&config)?, + SubCommand::Remove(x) => x.run(&config)?, SubCommand::Finish(x) => x.run(&config)?, } Ok(()) diff --git a/src/cmd.rs b/src/cmd.rs index 2f2f626..3de0e95 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -104,6 +104,21 @@ impl Edit { } } +#[derive(Debug, StructOpt)] +pub struct Remove { + /// Draft id. + draft: String, +} + +impl Remove { + pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { + let journal = Journal::new(&config.dirname, &config.entries)?; + let filename = journal.pick_draft(&self.draft)?; + journal.remove_draft(&filename)?; + Ok(()) + } +} + #[derive(Debug, StructOpt)] pub struct Finish { /// Draft id. diff --git a/src/error.rs b/src/error.rs index 36d8f07..04ba777 100644 --- a/src/error.rs +++ b/src/error.rs @@ -30,6 +30,10 @@ pub enum JournalError { #[error("failed to rename journal entry {0} to {1}: {2}")] RenameEntry(PathBuf, PathBuf, #[source] std::io::Error), + /// Failed to rename draft. + #[error("failed to remove draft {0}: {1}")] + RemoveDraft(PathBuf, #[source] std::io::Error), + /// Failed to write entry. #[error("failed to create journal entry {0}: {1}")] WriteEntry(PathBuf, #[source] std::io::Error), diff --git a/src/journal.rs b/src/journal.rs index ad8b4af..c12ebfa 100644 --- a/src/journal.rs +++ b/src/journal.rs @@ -163,6 +163,12 @@ impl Journal { Ok(()) } + pub fn remove_draft(&self, filename: &Path) -> Result<(), JournalError> { + std::fs::remove_file(filename) + .map_err(|err| JournalError::RemoveDraft(filename.to_path_buf(), err))?; + Ok(()) + } + pub fn finish_draft(&self, filename: &Path, basename: &str) -> Result<(), JournalError> { let entries = self.entries(); if !entries.exists() { diff --git a/src/opt.rs b/src/opt.rs index 7530f54..9170e0f 100644 --- a/src/opt.rs +++ b/src/opt.rs @@ -67,6 +67,9 @@ pub enum SubCommand { /// Invoke editor on journal entry draft. Edit(cmd::Edit), + /// Remove a journal entry draft. + Remove(cmd::Remove), + /// Finish a journal entry draft. Finish(cmd::Finish), } -- cgit v1.2.1