From: Ruben Beltran del Rio Date: Sat, 24 Feb 2024 23:20:14 +0000 (+0000) Subject: Add status command X-Git-Tag: 7.0.0~50 X-Git-Url: https://git.r.bdr.sh/rbdr/blog/commitdiff_plain/5f81d796fed4e4efbaf6dbed7d3c69481afddeb3?ds=sidebyside;hp=442ebaf901a8ca686ce769325054e1f393d43b7c Add status command --- diff --git a/src/command/add.rs b/src/command/add.rs index 4c80917..bc1bea0 100644 --- a/src/command/add.rs +++ b/src/command/add.rs @@ -1,4 +1,10 @@ use std::io::Result; +use super::{ + generate::Generate, + sync_down::SyncDown, + sync_up::SyncUp, + update::Update +}; pub struct Add; @@ -10,7 +16,7 @@ impl Add { impl super::Command for Add { fn before_dependencies(&self) -> Vec> { - vec![] + vec![Box::new(SyncDown::new())] } fn execute(&self, input: Option<&String>) -> Result<()> { @@ -19,7 +25,11 @@ impl super::Command for Add { } fn after_dependencies(&self) -> Vec> { - vec![] + vec![ + Box::new(Update::new()), + Box::new(Generate::new()), + Box::new(SyncUp::new()) + ] } fn command(&self) -> &'static str { diff --git a/src/command/help.rs b/src/command/help.rs index 628fd5a..7ddcba8 100644 --- a/src/command/help.rs +++ b/src/command/help.rs @@ -1,4 +1,3 @@ -use std::env::args; use std::io::Result; use super::available_commands; @@ -16,13 +15,11 @@ impl super::Command for Help { } fn execute(&self, _: Option<&String>) -> Result<()> { - let arguments: Vec = args().collect(); - let app_name = arguments.get(0).map_or("blog", |s| s.as_str()); let commands = available_commands(); println!("Usage:"); println!(""); for command in commands { - print!("{} {} ", app_name, command.command()); + print!("blog {} ", command.command()); println!("{}", command.help()); } return Ok(()) diff --git a/src/command/mod.rs b/src/command/mod.rs index c207754..211b10f 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -7,9 +7,11 @@ pub mod add_remote; pub mod remove_remote; pub mod sync_up; pub mod sync_down; +pub mod status; pub mod version; pub mod help; + use std::io::Result; use add::Add; @@ -22,6 +24,7 @@ use remove_remote::RemoveRemote; use sync_up::SyncUp; use sync_down::SyncDown; use version::Version; +use status::Status; use help::Help; pub trait Command { @@ -43,6 +46,7 @@ pub fn available_commands() -> Vec> { Box::new(RemoveRemote::new()), Box::new(SyncUp::new()), Box::new(SyncDown::new()), + Box::new(Status::new()), Box::new(Version::new()), Box::new(Help::new()) ] diff --git a/src/command/status/configuration_status.rs b/src/command/status/configuration_status.rs new file mode 100644 index 0000000..67bf277 --- /dev/null +++ b/src/command/status/configuration_status.rs @@ -0,0 +1,38 @@ +use std::fs; +use std::path::PathBuf; +use crate::configuration::Configuration; + +pub fn status() -> String { + let configuration = Configuration::new(); + let mut status_message = String::new(); + + status_message.push_str("# Configuration\n"); + status_message.push_str("## Directories\n"); + + // Main Configuration Locations + status_message.push_str(&get_directory_stats("Configuration", configuration.config_directory)); + status_message.push_str(&get_directory_stats("Data", configuration.data_directory)); + status_message.push_str(&get_directory_stats("Output", configuration.output_directory)); + + status_message.push_str("## Blog Settings\n"); + status_message.push_str(&format!("Number of posts to keep: {}\n", configuration.max_posts)); + status_message +} + +fn get_directory_stats(label: &str, directory: PathBuf) -> String { + let mut status_message = String::new(); + + status_message.push_str(&format!("{}: {}\n", label, directory.display())); + if directory.exists() { + status_message.push_str(&format!("{} directory exists.\n", label)); + if fs::read_dir(&directory).is_ok() { + status_message.push_str(&format!("{} directory is readable.\n", label)); + } else { + status_message.push_str(&format!("{} directory is not readable.\n", label)); + } + } else { + status_message.push_str(&format!("{} directory does not exist.\n", label)); + } + + status_message +} diff --git a/src/command/status/mod.rs b/src/command/status/mod.rs new file mode 100644 index 0000000..ba73184 --- /dev/null +++ b/src/command/status/mod.rs @@ -0,0 +1,43 @@ +mod configuration_status; + +use std::io::Result; + +pub struct Status; + +impl Status { + pub fn new() -> Self { + Status + } +} + +impl super::Command for Status { + fn before_dependencies(&self) -> Vec> { + vec![] + } + + fn execute(&self, _: Option<&String>) -> Result<()> { + let status_providers = available_status_providers(); + for status_provider in status_providers { + println!("{}", status_provider()); + } + return Ok(()) + } + + fn after_dependencies(&self) -> Vec> { + vec![] + } + + fn command(&self) -> &'static str { + "status" + } + + fn help(&self) -> &'static str { + "\t\t\t\tPrints the status of your blog" + } +} + +fn available_status_providers() -> Vec String> { + vec![ + configuration_status::status, + ] +} diff --git a/src/command/update.rs b/src/command/update.rs index d251093..0a0211c 100644 --- a/src/command/update.rs +++ b/src/command/update.rs @@ -1,4 +1,5 @@ use std::io::Result; +use super::{sync_down::SyncDown, generate::Generate, sync_up::SyncUp}; pub struct Update; @@ -10,7 +11,7 @@ impl Update { impl super::Command for Update { fn before_dependencies(&self) -> Vec> { - vec![] + vec![Box::new(SyncDown::new())] } fn execute(&self, input: Option<&String>) -> Result<()> { @@ -19,7 +20,10 @@ impl super::Command for Update { } fn after_dependencies(&self) -> Vec> { - vec![] + vec![ + Box::new(Generate::new()), + Box::new(SyncUp::new()) + ] } fn command(&self) -> &'static str { diff --git a/src/command/version.rs b/src/command/version.rs index fae7596..a8b970b 100644 --- a/src/command/version.rs +++ b/src/command/version.rs @@ -13,8 +13,9 @@ impl super::Command for Version { vec![] } - fn execute(&self, input: Option<&String>) -> Result<()> { - println!("Version: {:?}!", input); + fn execute(&self, _: Option<&String>) -> Result<()> { + let version = env!("CARGO_PKG_VERSION"); + println!("{}", version); return Ok(()) } diff --git a/src/main.rs b/src/main.rs index cb258c2..bf1e184 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,24 +2,34 @@ mod configuration; mod command; +use std::iter::once; use std::env::args; use std::io::Result; -use configuration::Configuration; use command::{available_commands, Command, help::Help}; fn main() -> Result<()> { - let configuration = Configuration::new(); let commands = available_commands(); - - println!("CONFIGURATION DIRECTORY: {}", configuration.config_directory.display()); - println!("DATA DIRECTORY: {}", configuration.data_directory.display()); - println!("OUTPUT DIRECTORY: {}", configuration.output_directory.display()); - let arguments: Vec = args().collect(); if let Some(command_name) = arguments.get(1) { - if let Some(command) = commands.iter().find(|&c| c.command() == command_name) { - return command.execute(arguments.get(2)); + if let Some(main_command) = commands.into_iter().find(|c| c.command() == command_name) { + let before_commands = main_command.before_dependencies(); + let after_commands = main_command.after_dependencies(); + + let command_chain: Vec> = before_commands + .into_iter() + .chain(once(main_command)) + .chain(after_commands.into_iter()) + .collect(); + + for command in command_chain { + let result = command.execute(arguments.get(2)); + if let Err(_) = result { + return result; + } + } + + return Ok(()) } }