summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers+gitlab@digital-scurf.org>2021-07-20 07:51:07 +0000
committerDaniel Silverstone <dsilvers+gitlab@digital-scurf.org>2021-07-20 07:51:07 +0000
commitd73806f722113116d7a5b7886df2b7d5ccb34cda (patch)
tree631b6ba9e8868b07c9447da90c002eac200e0801
parentda1826ddfb71a0596b09e04de10b33b9d1e60c7d (diff)
parent86050e18b21181a4006fd1a5b872c792e7900683 (diff)
downloadjt2-d73806f722113116d7a5b7886df2b7d5ccb34cda.tar.gz
Merge branch 'remove' into 'main'
feat: add command to remove a draft Closes #12 See merge request larswirzenius/jt!21
-rw-r--r--jt.md30
-rw-r--r--src/bin/jt2.rs1
-rw-r--r--src/cmd.rs15
-rw-r--r--src/error.rs4
-rw-r--r--src/journal.rs6
-rw-r--r--src/opt.rs3
6 files changed, 59 insertions, 0 deletions
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
@@ -105,6 +105,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.
draft: String,
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),
}