]> git.r.bdr.sh - rbdr/blog/commitdiff
Add status command
authorRuben Beltran del Rio <redacted>
Sat, 24 Feb 2024 23:20:14 +0000 (23:20 +0000)
committerRuben Beltran del Rio <redacted>
Sat, 24 Feb 2024 23:20:14 +0000 (23:20 +0000)
src/command/add.rs
src/command/help.rs
src/command/mod.rs
src/command/status/configuration_status.rs [new file with mode: 0644]
src/command/status/mod.rs [new file with mode: 0644]
src/command/update.rs
src/command/version.rs
src/main.rs

index 4c809177756e6578360e2a90a60084643c769dbf..bc1bea0bb06c86f2077b374b7de68a51cbd0cb5b 100644 (file)
@@ -1,4 +1,10 @@
 use std::io::Result;
 use std::io::Result;
+use super::{
+    generate::Generate,
+    sync_down::SyncDown,
+    sync_up::SyncUp,
+    update::Update
+};
 
 pub struct Add;
 
 
 pub struct Add;
 
@@ -10,7 +16,7 @@ impl Add {
 
 impl super::Command for Add {
     fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
 
 impl super::Command for Add {
     fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
-        vec![]
+        vec![Box::new(SyncDown::new())]
     }
 
     fn execute(&self, input: Option<&String>) -> Result<()> {
     }
 
     fn execute(&self, input: Option<&String>) -> Result<()> {
@@ -19,7 +25,11 @@ impl super::Command for Add {
     }
 
     fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
     }
 
     fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
-        vec![]
+        vec![
+            Box::new(Update::new()),
+            Box::new(Generate::new()),
+            Box::new(SyncUp::new())
+        ]
     }
 
     fn command(&self) -> &'static str {
     }
 
     fn command(&self) -> &'static str {
index 628fd5aa81a25a5371de0cfc74ab81e22f6f2c3e..7ddcba8f5b9d6b275b1bdf0a8f625380d3f0d65f 100644 (file)
@@ -1,4 +1,3 @@
-use std::env::args;
 use std::io::Result;
 use super::available_commands;
 
 use std::io::Result;
 use super::available_commands;
 
@@ -16,13 +15,11 @@ impl super::Command for Help {
     }
 
     fn execute(&self, _: Option<&String>) -> Result<()> {
     }
 
     fn execute(&self, _: Option<&String>) -> Result<()> {
-        let arguments: Vec<String> = 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 {
         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(())
             println!("{}", command.help());
         }
         return Ok(())
index c2077541b1acf97a320f00668300c822390b36c8..211b10f6dbf757da70a02eb127597ec90738b2d5 100644 (file)
@@ -7,9 +7,11 @@ pub mod add_remote;
 pub mod remove_remote;
 pub mod sync_up;
 pub mod sync_down;
 pub mod remove_remote;
 pub mod sync_up;
 pub mod sync_down;
+pub mod status;
 pub mod version;
 pub mod help;
 
 pub mod version;
 pub mod help;
 
+
 use std::io::Result;
 
 use add::Add;
 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 sync_up::SyncUp;
 use sync_down::SyncDown;
 use version::Version;
+use status::Status;
 use help::Help;
 
 pub trait Command {
 use help::Help;
 
 pub trait Command {
@@ -43,6 +46,7 @@ pub fn available_commands() -> Vec<Box<dyn Command>> {
         Box::new(RemoveRemote::new()),
         Box::new(SyncUp::new()),
         Box::new(SyncDown::new()),
         Box::new(RemoveRemote::new()),
         Box::new(SyncUp::new()),
         Box::new(SyncDown::new()),
+        Box::new(Status::new()),
         Box::new(Version::new()),
         Box::new(Help::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 (file)
index 0000000..67bf277
--- /dev/null
@@ -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 (file)
index 0000000..ba73184
--- /dev/null
@@ -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<Box<dyn super::Command>> {
+        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<Box<dyn super::Command>> {
+        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<fn() -> String> {
+    vec![
+        configuration_status::status,
+    ]
+}
index d25109365a189a4a8bdaeba571f099dd38903276..0a0211cbae485729691120850c88dca78c01a4f9 100644 (file)
@@ -1,4 +1,5 @@
 use std::io::Result;
 use std::io::Result;
+use super::{sync_down::SyncDown, generate::Generate, sync_up::SyncUp};
 
 pub struct Update;
 
 
 pub struct Update;
 
@@ -10,7 +11,7 @@ impl Update {
 
 impl super::Command for Update {
     fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
 
 impl super::Command for Update {
     fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
-        vec![]
+        vec![Box::new(SyncDown::new())]
     }
 
     fn execute(&self, input: Option<&String>) -> Result<()> {
     }
 
     fn execute(&self, input: Option<&String>) -> Result<()> {
@@ -19,7 +20,10 @@ impl super::Command for Update {
     }
 
     fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
     }
 
     fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
-        vec![]
+        vec![
+            Box::new(Generate::new()),
+            Box::new(SyncUp::new())
+        ]
     }
 
     fn command(&self) -> &'static str {
     }
 
     fn command(&self) -> &'static str {
index fae75961bc79957394ad67ea92679a9c21729164..a8b970b627eafdc74f6a7dc062305b3aa663250b 100644 (file)
@@ -13,8 +13,9 @@ impl super::Command for Version {
         vec![]
     }
 
         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(())
     }
 
         return Ok(())
     }
 
index cb258c277f8257e684f48f64cb6c9a8b947d4927..bf1e1844a2162646f2046f728af45de84a187aef 100644 (file)
@@ -2,24 +2,34 @@
 mod configuration;
 mod command;
 
 mod configuration;
 mod command;
 
+use std::iter::once;
 use std::env::args;
 use std::io::Result;
 use std::env::args;
 use std::io::Result;
-use configuration::Configuration;
 use command::{available_commands, Command, help::Help};
 
 fn main() -> Result<()> {
 use command::{available_commands, Command, help::Help};
 
 fn main() -> Result<()> {
-    let configuration = Configuration::new();
     let commands = available_commands();
     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<String> = args().collect();
 
     if let Some(command_name) = arguments.get(1) {
     let arguments: Vec<String> = 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<Box<dyn Command>> = 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(())
         }
     }
 
         }
     }