diff options
Diffstat (limited to 'src/bin/jt2.rs')
-rw-r--r-- | src/bin/jt2.rs | 63 |
1 files changed, 7 insertions, 56 deletions
diff --git a/src/bin/jt2.rs b/src/bin/jt2.rs index 677bb35..8ef0728 100644 --- a/src/bin/jt2.rs +++ b/src/bin/jt2.rs @@ -1,9 +1,6 @@ use jt2::config::Configuration; -use jt2::error::JournalError; -use jt2::journal::Journal; use jt2::opt::{Opt, SubCommand}; -use std::path::{Path, PathBuf}; use structopt::StructOpt; fn main() -> anyhow::Result<()> { @@ -11,59 +8,13 @@ fn main() -> anyhow::Result<()> { let opt = Opt::from_args(); let config = Configuration::read(&opt)?; match opt.cmd { - SubCommand::Config => config.dump(), - SubCommand::Init { - journalname, - description, - } => init(&config.dirname, &journalname, &description, &config)?, - SubCommand::IsJournal => is_journal(&config)?, - SubCommand::New { title, topic } => new_draft(&title, &topic, &config)?, - SubCommand::NewTopic { path, title } => new_topic(&path, &title, &config)?, - SubCommand::Edit { draft } => edit_draft(&draft, &config)?, - SubCommand::Finish { draft, basename } => finish_draft(&draft, &basename, &config)?, + SubCommand::Config(x) => x.run(&config)?, + SubCommand::Init(x) => x.run(&config)?, + SubCommand::IsJournal(x) => x.run(&config)?, + SubCommand::New(x) => x.run(&config)?, + SubCommand::NewTopic(x) => x.run(&config)?, + SubCommand::Edit(x) => x.run(&config)?, + SubCommand::Finish(x) => x.run(&config)?, } Ok(()) } - -fn init( - dirname: &Path, - _journalname: &str, - _description: &str, - config: &Configuration, -) -> anyhow::Result<()> { - Journal::init(dirname, &config.entries)?; - Ok(()) -} - -fn is_journal(config: &Configuration) -> anyhow::Result<()> { - if !Journal::is_journal(&config.dirname, &config.entries) { - return Err(JournalError::NotAJournal(config.dirname.display().to_string()).into()); - } - Ok(()) -} - -fn new_draft(title: &str, topic: &Option<PathBuf>, config: &Configuration) -> anyhow::Result<()> { - let journal = Journal::new(&config.dirname, &config.entries)?; - journal.new_draft(title, topic, &config.editor)?; - Ok(()) -} - -fn new_topic(path: &Path, title: &str, config: &Configuration) -> anyhow::Result<()> { - let journal = Journal::new(&config.dirname, &config.entries)?; - journal.new_topic(path, title, &config.editor)?; - Ok(()) -} - -fn edit_draft(draft: &str, config: &Configuration) -> anyhow::Result<()> { - let journal = Journal::new(&config.dirname, &config.entries)?; - let filename = journal.pick_draft(draft)?; - journal.edit_draft(&config.editor, &filename)?; - Ok(()) -} - -fn finish_draft(draft: &str, basename: &str, config: &Configuration) -> anyhow::Result<()> { - let journal = Journal::new(&config.dirname, &config.entries)?; - let filename = journal.pick_draft(draft)?; - journal.finish_draft(&filename, basename)?; - Ok(()) -} |