From b38bb186b1d6ce0cf19d66a89f8afdf0d78bcb80 Mon Sep 17 00:00:00 2001 From: quirinecker Date: Thu, 19 Jan 2023 17:12:48 +0100 Subject: [PATCH] initial commit (proof of concept build function, added cli structure --- .gitignore | 2 + Cargo.lock | 7 + Cargo.toml | 8 + docs/core/functions.adoc | 0 docs/core/functions.html | 451 +++++++++++++++++++++++++++++++++++++ docs/core/index.adoc | 0 docs/index.adoc | 0 src/app/commands/build.rs | 67 ++++++ src/app/commands/mod.rs | 40 ++++ src/app/commands/traits.rs | 9 + src/app/mod.rs | 66 ++++++ src/main.rs | 11 + 12 files changed, 661 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 docs/core/functions.adoc create mode 100644 docs/core/functions.html create mode 100644 docs/core/index.adoc create mode 100644 docs/index.adoc create mode 100644 src/app/commands/build.rs create mode 100644 src/app/commands/mod.rs create mode 100644 src/app/commands/traits.rs create mode 100644 src/app/mod.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..77fdd3e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +dist diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..158cf5d --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "docki-cli" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..cac466a --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "docki-cli" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/docs/core/functions.adoc b/docs/core/functions.adoc new file mode 100644 index 0000000..e69de29 diff --git a/docs/core/functions.html b/docs/core/functions.html new file mode 100644 index 0000000..78b1650 --- /dev/null +++ b/docs/core/functions.html @@ -0,0 +1,451 @@ + + + + + + + +Untitled + + + + + +
+ +
+ + + \ No newline at end of file diff --git a/docs/core/index.adoc b/docs/core/index.adoc new file mode 100644 index 0000000..e69de29 diff --git a/docs/index.adoc b/docs/index.adoc new file mode 100644 index 0000000..e69de29 diff --git a/src/app/commands/build.rs b/src/app/commands/build.rs new file mode 100644 index 0000000..42361af --- /dev/null +++ b/src/app/commands/build.rs @@ -0,0 +1,67 @@ +use std::{collections::HashMap, env, fs, process}; + +use super::traits::Command; + +pub struct Build; +impl Build { + fn build_dir(&self, path: String) -> Result<(), String> { + let Ok(dirs) = fs::read_dir(path) else { + return Err("could not read file system".to_string()); + }; + + for result in dirs { + let Ok(entry) = result else { + return Err("could not read entry".to_string()); + }; + + if entry.path().is_dir() { + self.build_dir(entry.path().to_str().unwrap().to_string()); + } else { + self.build_file(entry.path().to_str().unwrap().to_string()); + } + } + + return Err("No files to build".to_string()); + } + + fn build_file(&self, path: String) -> Result<(), String> { + let out_path = path + .clone() + .replace("docs", "dist") + .replace(".adoc", ".html"); + + println!("asciidoctor {path} --out-file={out_path}"); + let _result = process::Command::new("asciidoctor") + .arg(format!("{path}")) + .arg(format!("--out-file={out_path}")) + .output() + .expect("fuck"); + + dbg!(_result); + + return Ok(()); + } +} + +impl Command for Build { + fn execute(&self, _args: &HashMap) -> Result<(), String> { + let Ok(project_cwd_object) = env::current_dir() else { + return Err("current dirctory does not seem to exist".to_string()) + }; + + let Some(project_cwd) = project_cwd_object.to_str() else { + return Err("invalid path".to_string()); + }; + + let path = format!("{project_cwd}/docs/"); + + return self.build_dir(path); + } + + fn new() -> Self + where + Self: Sized, + { + return Build {}; + } +} diff --git a/src/app/commands/mod.rs b/src/app/commands/mod.rs new file mode 100644 index 0000000..d7f1263 --- /dev/null +++ b/src/app/commands/mod.rs @@ -0,0 +1,40 @@ +use std::collections::HashMap; + +use traits::Command; + +use self::build::Build; + +pub mod traits; +mod build; + +pub struct CommandRegistry { + commands: HashMap> +} + +impl CommandRegistry { + + pub fn register_all(&mut self) { + let registry = self; + registry.register("/build".to_string(), Box::new(Build::new()), true) + } + + pub fn register(&mut self, path: String, command: Box, enabled: bool) { + if enabled { + self.commands.insert(path, command); + } + } + + pub fn new() -> CommandRegistry { + let mut registry = CommandRegistry { commands: HashMap::new() }; + + registry.register_all(); + + registry + } + + pub fn command_by(&self, path: &String) -> Option<&Box> { + let command = self.commands.get(path); + return command; + } +} + diff --git a/src/app/commands/traits.rs b/src/app/commands/traits.rs new file mode 100644 index 0000000..97d0c03 --- /dev/null +++ b/src/app/commands/traits.rs @@ -0,0 +1,9 @@ +use std::collections::HashMap; + +pub trait Command { + fn execute(&self, args: &HashMap) -> Result<(), String>; + fn new() -> Self where Self: Sized; +} + + + diff --git a/src/app/mod.rs b/src/app/mod.rs new file mode 100644 index 0000000..0fef376 --- /dev/null +++ b/src/app/mod.rs @@ -0,0 +1,66 @@ +mod commands; + +use std::collections::HashMap; + +use commands::traits::Command; +use commands::CommandRegistry; + +pub struct App { + command_regisrty: CommandRegistry, +} + +impl App { + pub fn new() -> App { + return App { + command_regisrty: CommandRegistry::new() + } + } + + pub fn start(&self, args: Vec) { + let command_args = &args[1..]; + let mut path = String::from(""); + let mut argument_map = HashMap::new(); + let mut only_options_left = false; + + for (index, argument) in command_args.iter().enumerate() { + if argument.starts_with("--") { + only_options_left = true; + let value = command_args.get(index + 1); + if let Some(v) = value { + if v.starts_with("--") { + argument_map.insert(argument.replace("--", ""), String::from("")); + } else { + argument_map.insert(argument.replace("--", ""), String::from(v)); + } + } else { + argument_map.insert(argument.replace("--", ""), String::from("")); + } + } else if !only_options_left { + path.push_str(&format!("/{argument}")) + } + } + + self.execute_path(&path, &argument_map); + } + + fn execute_path(&self, path: &String, args: &HashMap) { + let command = self.command_regisrty.command_by(path); + + if let Some(c) = command { + self.execute_command(c, args) + } else { + println!("command not found") + } + } + + fn execute_command(&self, c: &Box, args: &HashMap) { + let result = c.execute(args); + + match result { + Ok(_) => println!("successfully executed"), + Err(message) => println!("{message}"), + } + } +} + + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..11311b0 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,11 @@ +mod app; + +use std::env; + +use app::App; + +fn main() { + let app = App::new(); + let args = env::args().collect(); + app.start(args); +}