aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2024-02-23 20:37:39 +0000
committerRuben Beltran del Rio <git@r.bdr.sh>2024-02-23 20:37:39 +0000
commitd620665f6b2e1ae5db4c98a09e35bd63133ae87f (patch)
tree964bf044d3e1ae880c65d252e7c702f81874f2bc /src
parent72c91149425d45b0517bda929d459fc02f5603cc (diff)
Add logic skeleton for rust version
Diffstat (limited to 'src')
-rw-r--r--src/command/add.rs32
-rw-r--r--src/command/add_remote.rs32
-rw-r--r--src/command/generate.rs32
-rw-r--r--src/command/help.rs32
-rw-r--r--src/command/mod.rs49
-rw-r--r--src/command/publish.rs32
-rw-r--r--src/command/publish_archive.rs32
-rw-r--r--src/command/remove_remote.rs32
-rw-r--r--src/command/sync_down.rs33
-rw-r--r--src/command/sync_up.rs32
-rw-r--r--src/command/update.rs33
-rw-r--r--src/command/version.rs32
-rw-r--r--src/configuration.rs91
-rw-r--r--src/main.rs27
14 files changed, 521 insertions, 0 deletions
diff --git a/src/command/add.rs b/src/command/add.rs
new file mode 100644
index 0000000..36f25a0
--- /dev/null
+++ b/src/command/add.rs
@@ -0,0 +1,32 @@
+use std::io::Result;
+
+pub struct Add;
+
+impl Add {
+ pub fn new() -> Self {
+ Add
+ }
+}
+
+impl super::Command for Add {
+ fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn execute(&self, input: Option<&String>) -> Result<()> {
+ println!("Add: {:?}!", input);
+ return Ok(())
+ }
+
+ fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn command(&self) -> &'static str {
+ "add"
+ }
+
+ fn help(&self) -> &'static str {
+ "<path_to_post>\t\tCreates new blog post"
+ }
+}
diff --git a/src/command/add_remote.rs b/src/command/add_remote.rs
new file mode 100644
index 0000000..3d90103
--- /dev/null
+++ b/src/command/add_remote.rs
@@ -0,0 +1,32 @@
+use std::io::Result;
+
+pub struct AddRemote;
+
+impl AddRemote {
+ pub fn new() -> Self {
+ AddRemote
+ }
+}
+
+impl super::Command for AddRemote {
+ fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn execute(&self, input: Option<&String>) -> Result<()> {
+ println!("Add Remote: {:?}!", input);
+ return Ok(())
+ }
+
+ fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn command(&self) -> &'static str {
+ "add-remote"
+ }
+
+ fn help(&self) -> &'static str {
+ "<git_url>\t\tAdds or updates a git remote to sync with"
+ }
+}
diff --git a/src/command/generate.rs b/src/command/generate.rs
new file mode 100644
index 0000000..943f2be
--- /dev/null
+++ b/src/command/generate.rs
@@ -0,0 +1,32 @@
+use std::io::Result;
+
+pub struct Generate;
+
+impl Generate {
+ pub fn new() -> Self {
+ Generate
+ }
+}
+
+impl super::Command for Generate {
+ fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn execute(&self, input: Option<&String>) -> Result<()> {
+ println!("GENERATE! {:?}", input);
+ return Ok(())
+ }
+
+ fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn command(&self) -> &'static str {
+ "generate"
+ }
+
+ fn help(&self) -> &'static str {
+ "\t\t\tGenerates the blog assets"
+ }
+}
diff --git a/src/command/help.rs b/src/command/help.rs
new file mode 100644
index 0000000..9b98393
--- /dev/null
+++ b/src/command/help.rs
@@ -0,0 +1,32 @@
+use std::io::Result;
+
+pub struct Help;
+
+impl Help {
+ pub fn new() -> Self {
+ Help
+ }
+}
+
+impl super::Command for Help {
+ fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn execute(&self, input: Option<&String>) -> Result<()> {
+ println!("Help: {:?}!", input);
+ return Ok(())
+ }
+
+ fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn command(&self) -> &'static str {
+ "help"
+ }
+
+ fn help(&self) -> &'static str {
+ "\t\t\t\tPrints this help"
+ }
+}
diff --git a/src/command/mod.rs b/src/command/mod.rs
new file mode 100644
index 0000000..c207754
--- /dev/null
+++ b/src/command/mod.rs
@@ -0,0 +1,49 @@
+pub mod add;
+pub mod generate;
+pub mod update;
+pub mod publish;
+pub mod publish_archive;
+pub mod add_remote;
+pub mod remove_remote;
+pub mod sync_up;
+pub mod sync_down;
+pub mod version;
+pub mod help;
+
+use std::io::Result;
+
+use add::Add;
+use generate::Generate;
+use update::Update;
+use publish::Publish;
+use publish_archive::PublishArchive;
+use add_remote::AddRemote;
+use remove_remote::RemoveRemote;
+use sync_up::SyncUp;
+use sync_down::SyncDown;
+use version::Version;
+use help::Help;
+
+pub trait Command {
+ fn before_dependencies(&self) -> Vec<Box<dyn Command>>;
+ fn execute(&self, input: Option<&String>) -> Result<()>;
+ fn after_dependencies(&self) -> Vec<Box<dyn Command>>;
+ fn command(&self) -> &'static str;
+ fn help(&self) -> &'static str;
+}
+
+pub fn available_commands() -> Vec<Box<dyn Command>> {
+ vec![
+ Box::new(Add::new()),
+ Box::new(Generate::new()),
+ Box::new(Update::new()),
+ Box::new(Publish::new()),
+ Box::new(PublishArchive::new()),
+ Box::new(AddRemote::new()),
+ Box::new(RemoveRemote::new()),
+ Box::new(SyncUp::new()),
+ Box::new(SyncDown::new()),
+ Box::new(Version::new()),
+ Box::new(Help::new())
+ ]
+}
diff --git a/src/command/publish.rs b/src/command/publish.rs
new file mode 100644
index 0000000..d8ca949
--- /dev/null
+++ b/src/command/publish.rs
@@ -0,0 +1,32 @@
+use std::io::Result;
+
+pub struct Publish;
+
+impl Publish {
+ pub fn new() -> Self {
+ Publish
+ }
+}
+
+impl super::Command for Publish {
+ fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn execute(&self, input: Option<&String>) -> Result<()> {
+ println!("Publish: {:?}!", input);
+ return Ok(())
+ }
+
+ fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn command(&self) -> &'static str {
+ "publish"
+ }
+
+ fn help(&self) -> &'static str {
+ "<destination>\tPublishes the blog to a remote host."
+ }
+}
diff --git a/src/command/publish_archive.rs b/src/command/publish_archive.rs
new file mode 100644
index 0000000..4275c38
--- /dev/null
+++ b/src/command/publish_archive.rs
@@ -0,0 +1,32 @@
+use std::io::Result;
+
+pub struct PublishArchive;
+
+impl PublishArchive {
+ pub fn new() -> Self {
+ PublishArchive
+ }
+}
+
+impl super::Command for PublishArchive {
+ fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn execute(&self, input: Option<&String>) -> Result<()> {
+ println!("Publish Archive: {:?}!", input);
+ return Ok(())
+ }
+
+ fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn command(&self) -> &'static str {
+ "publish-archive"
+ }
+
+ fn help(&self) -> &'static str {
+ "<destination>\tPublishes the archive to a remote host."
+ }
+}
diff --git a/src/command/remove_remote.rs b/src/command/remove_remote.rs
new file mode 100644
index 0000000..cfc5cdb
--- /dev/null
+++ b/src/command/remove_remote.rs
@@ -0,0 +1,32 @@
+use std::io::Result;
+
+pub struct RemoveRemote;
+
+impl RemoveRemote {
+ pub fn new() -> Self {
+ RemoveRemote
+ }
+}
+
+impl super::Command for RemoveRemote {
+ fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn execute(&self, input: Option<&String>) -> Result<()> {
+ println!("Remove Remote: {:?}!", input);
+ return Ok(())
+ }
+
+ fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn command(&self) -> &'static str {
+ "remove-remote"
+ }
+
+ fn help(&self) -> &'static str {
+ "\t\t\tRemoves the git remote"
+ }
+}
diff --git a/src/command/sync_down.rs b/src/command/sync_down.rs
new file mode 100644
index 0000000..a0c713b
--- /dev/null
+++ b/src/command/sync_down.rs
@@ -0,0 +1,33 @@
+use std::io::Result;
+
+pub struct SyncDown;
+
+impl SyncDown {
+ pub fn new() -> Self {
+ SyncDown
+ }
+}
+
+impl super::Command for SyncDown {
+ fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn execute(&self, input: Option<&String>) -> Result<()> {
+ println!("Sync Down: {:?}!", input);
+ return Ok(())
+ }
+
+ fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn command(&self) -> &'static str {
+ "sync-down"
+ }
+
+ fn help(&self) -> &'static str {
+ "\t\t\tPulls from the git remote if configured"
+ }
+}
+
diff --git a/src/command/sync_up.rs b/src/command/sync_up.rs
new file mode 100644
index 0000000..cf49518
--- /dev/null
+++ b/src/command/sync_up.rs
@@ -0,0 +1,32 @@
+use std::io::Result;
+
+pub struct SyncUp;
+
+impl SyncUp {
+ pub fn new() -> Self {
+ SyncUp
+ }
+}
+
+impl super::Command for SyncUp {
+ fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn execute(&self, input: Option<&String>) -> Result<()> {
+ println!("Sync Up: {:?}!", input);
+ return Ok(())
+ }
+
+ fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn command(&self) -> &'static str {
+ "sync-up"
+ }
+
+ fn help(&self) -> &'static str {
+ "\t\t\t\tPushes to the git remote if configured."
+ }
+}
diff --git a/src/command/update.rs b/src/command/update.rs
new file mode 100644
index 0000000..d251093
--- /dev/null
+++ b/src/command/update.rs
@@ -0,0 +1,33 @@
+use std::io::Result;
+
+pub struct Update;
+
+impl Update {
+ pub fn new() -> Self {
+ Update
+ }
+}
+
+impl super::Command for Update {
+ fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn execute(&self, input: Option<&String>) -> Result<()> {
+ println!("Update: {:?}!", input);
+ return Ok(())
+ }
+
+ fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn command(&self) -> &'static str {
+ "update"
+ }
+
+ fn help(&self) -> &'static str {
+ "<path_to_post>\t\tUpdates latest blog post"
+ }
+}
+
diff --git a/src/command/version.rs b/src/command/version.rs
new file mode 100644
index 0000000..fae7596
--- /dev/null
+++ b/src/command/version.rs
@@ -0,0 +1,32 @@
+use std::io::Result;
+
+pub struct Version;
+
+impl Version {
+ pub fn new() -> Self {
+ Version
+ }
+}
+
+impl super::Command for Version {
+ fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn execute(&self, input: Option<&String>) -> Result<()> {
+ println!("Version: {:?}!", input);
+ return Ok(())
+ }
+
+ fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
+ vec![]
+ }
+
+ fn command(&self) -> &'static str {
+ "version"
+ }
+
+ fn help(&self) -> &'static str {
+ "\t\t\t\tPrints the version"
+ }
+}
diff --git a/src/configuration.rs b/src/configuration.rs
new file mode 100644
index 0000000..65eb0ba
--- /dev/null
+++ b/src/configuration.rs
@@ -0,0 +1,91 @@
+use std::env;
+use std::path::PathBuf;
+
+pub struct Configuration {
+ // Default Base Directories, default to XDG dirs but can be
+ // configured by users.
+ pub config_directory: PathBuf,
+ pub data_directory: PathBuf,
+ pub output_directory: PathBuf,
+
+ pub max_posts: u8,
+
+ // Derived directories: Config
+ pub remote_config: PathBuf,
+
+ // Derived directories: Data
+ pub posts_directory: PathBuf,
+ pub archive_directory: PathBuf,
+ pub static_directory: PathBuf,
+ pub templates_directory: PathBuf,
+
+ // Derived directories: Output
+ pub blog_output_directory: PathBuf,
+ pub archive_output_directory: PathBuf
+}
+
+impl Configuration {
+
+ pub fn new() -> Self {
+ let config_directory = Configuration::directory(
+ "BLOG_CONFIG_DIRECTORY",
+ "XDG_CONFIG_HOME",
+ ".config",
+ "blog"
+ );
+ let data_directory = Configuration::directory(
+ "BLOG_DATA_DIRECTORY",
+ "XDG_DATA_HOME",
+ ".local/share",
+ "blog"
+ );
+ let output_directory = Configuration::directory(
+ "BLOG_OUTPUT_DIRECTORY",
+ "XDG_CACHE_HOME",
+ ".cache",
+ "blog"
+ );
+
+ let max_posts: u8 = env::var("BLOG_MAX_POSTS")
+ .ok()
+ .and_then(|v| v.parse().ok())
+ .unwrap_or(3);
+
+ let remote_config = config_directory.join("blogremote");
+
+ let posts_directory = data_directory.join("posts");
+ let archive_directory = data_directory.join("archive");
+ let static_directory = data_directory.join("static");
+ let templates_directory = data_directory.join("templates");
+
+ let blog_output_directory = output_directory.join("blog");
+ let archive_output_directory = output_directory.join("archive");
+
+ Configuration {
+ config_directory,
+ data_directory,
+ output_directory,
+ max_posts,
+ remote_config,
+ posts_directory,
+ archive_directory,
+ static_directory,
+ templates_directory,
+ blog_output_directory,
+ archive_output_directory
+ }
+ }
+
+ fn directory(user_override: &str, default_value: &str, home_fallback: &str, path: &str) -> PathBuf {
+ match env::var(user_override) {
+ Ok(directory) => PathBuf::from(directory),
+ Err(_) => match env::var(default_value) {
+ Ok(directory) => PathBuf::from(directory),
+ Err(_) => match env::var("HOME") {
+ Ok(directory) => PathBuf::from(directory).join(home_fallback),
+ Err(_) => panic!("Could not find required directory, {} or {} should be set and readable.", user_override, default_value),
+ },
+ },
+ }.join(path)
+ }
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..cb258c2
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,27 @@
+// mod argument_parser;
+mod configuration;
+mod command;
+
+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<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));
+ }
+ }
+
+ Help::new().execute(None)
+}