summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-09-05 14:42:08 +0300
committerLars Wirzenius <liw@liw.fi>2020-09-05 18:31:23 +0300
commit33683712604607d85f20cbf93a7ac904552f4979 (patch)
treefc37020f3b0c8220e096c33d4f63008a612174c9 /src
parentbddb592c4cb89e76796b17dd5bed823eba7e2b0e (diff)
downloadjt2-33683712604607d85f20cbf93a7ac904552f4979.tar.gz
feat: create and initialise a new journal
At the moment this only creates a new directory, but most of the change is scaffolding so that more interesting changes can be built on top of this later.
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs1
-rw-r--r--src/main.rs54
2 files changed, 55 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1 @@
+
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..0a7efe1
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,54 @@
+use anyhow::Result;
+use std::fs;
+use std::path::{Path, PathBuf};
+use structopt::StructOpt;
+use thiserror::Error;
+
+#[derive(Debug, StructOpt)]
+#[structopt(about = "maintain a journal")]
+enum JT {
+ Init {
+ #[structopt(help = "Directory where journal should be stored")]
+ dirname: PathBuf,
+ #[structopt(help = "Short name for journal")]
+ journalname: String,
+ #[structopt(help = "Short description of journal, its title")]
+ description: String,
+ },
+ IsJournal {
+ #[structopt(help = "Directory that may or may not be a journal")]
+ dirname: PathBuf,
+ },
+}
+
+#[derive(Debug, Error)]
+enum JournalError {
+ #[error("directory {0} is not a journal")]
+ NotAJournal(String),
+}
+
+fn main() -> Result<()> {
+ let opt = JT::from_args();
+ match opt {
+ JT::Init {
+ dirname,
+ journalname,
+ description,
+ } => init(&dirname, &journalname, &description)?,
+ JT::IsJournal { dirname } => is_journal(&dirname)?,
+ }
+ Ok(())
+}
+
+fn init(dirname: &Path, _journalname: &str, _description: &str) -> anyhow::Result<()> {
+ std::fs::create_dir(dirname)?;
+ Ok(())
+}
+
+fn is_journal(dirname: &Path) -> anyhow::Result<()> {
+ let meta = fs::symlink_metadata(dirname)?;
+ if !meta.is_dir() {
+ return Err(JournalError::NotAJournal(dirname.to_string_lossy().to_string()).into());
+ }
+ Ok(())
+}