diff options
Diffstat (limited to 'src/cmd.rs')
-rw-r--r-- | src/cmd.rs | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/cmd.rs b/src/cmd.rs new file mode 100644 index 0000000..c1606e0 --- /dev/null +++ b/src/cmd.rs @@ -0,0 +1,110 @@ +use crate::config::Configuration; +use crate::error::JournalError; +use crate::journal::Journal; +use std::path::PathBuf; +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +pub struct Config {} + +impl Config { + pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { + config.dump(); + Ok(()) + } +} + +#[derive(Debug, StructOpt)] +pub struct Init { + #[structopt(help = "Short name for journal")] + journalname: String, + + #[structopt(help = "Short description of journal, its title")] + description: String, +} + +impl Init { + pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { + Journal::init(&config.dirname, &config.entries)?; + Ok(()) + } +} + +#[derive(Debug, StructOpt)] +pub struct IsJournal {} + +impl IsJournal { + pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { + if !Journal::is_journal(&config.dirname, &config.entries) { + return Err(JournalError::NotAJournal(config.dirname.display().to_string()).into()); + } + Ok(()) + } +} + +#[derive(Debug, StructOpt)] +pub struct New { + #[structopt(help = "Title of new draft")] + title: String, + + #[structopt(long, help = "Add link to a topic page")] + topic: Option<PathBuf>, +} + +impl New { + pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { + let journal = Journal::new(&config.dirname, &config.entries)?; + journal.new_draft(&self.title, &self.topic, &config.editor)?; + Ok(()) + } +} + +#[derive(Debug, StructOpt)] +pub struct NewTopic { + #[structopt(help = "Path to topic page in journal")] + path: PathBuf, + + #[structopt(help = "Title of topic page")] + title: String, +} + +impl NewTopic { + pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { + let journal = Journal::new(&config.dirname, &config.entries)?; + journal.new_topic(&self.path, &self.title, &config.editor)?; + Ok(()) + } +} + +#[derive(Debug, StructOpt)] +pub struct Edit { + /// Draft id. + draft: String, +} + +impl Edit { + pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { + let journal = Journal::new(&config.dirname, &config.entries)?; + let filename = journal.pick_draft(&self.draft)?; + journal.edit_draft(&config.editor, &filename)?; + Ok(()) + } +} + +#[derive(Debug, StructOpt)] +pub struct Finish { + /// Draft id. + draft: String, + + /// Set base name of published file. + basename: String, +} + +impl Finish { + pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { + let journal = Journal::new(&config.dirname, &config.entries)?; + let filename = journal.pick_draft(&self.draft)?; + journal.finish_draft(&filename, &self.basename)?; + Ok(()) + } +} |