summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-10-15 09:41:20 +0300
committerLars Wirzenius <liw@liw.fi>2020-10-20 08:44:42 +0300
commit02c53afea1ec6c6ae5060e01f908497a01f03981 (patch)
tree99b0dcd07c7826135fe331779f1e927595de3f0e /src
parentb3bcb11938f7bebdc15eef8fdb1e2267d39b2818 (diff)
downloadjt2-02c53afea1ec6c6ae5060e01f908497a01f03981.tar.gz
feat: create new draft, publish it
Diffstat (limited to 'src')
-rw-r--r--src/main.rs47
1 files changed, 47 insertions, 0 deletions
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(())
+}