refactored the project using the clap crate

This commit is contained in:
2023-03-08 21:35:15 +01:00
parent e469576d2c
commit 4b789e60c9
10 changed files with 92 additions and 233 deletions

View File

@@ -3,76 +3,32 @@ pub mod builder;
pub mod fs_util;
mod args;
use std::collections::HashMap;
use std::env;
use commands::traits::Command;
use commands::CommandRegistry;
use self::args::{args, structure::CommandArg};
use self::commands::build::build;
use self::commands::health::health;
use self::commands::install_reveal::install_reveal;
use self::args::args;
pub struct App {
command_regisrty: CommandRegistry,
}
pub struct App;
impl App {
pub fn new() -> App {
return App {
command_regisrty: CommandRegistry::new()
}
}
pub fn start(self, old_args: Vec<String>) {
pub fn start(&self) {
let args = args();
Self::preapare_env_path();
let command_args = &old_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 preapare_env_path() {
env::set_var("PATH", fs_util::docki_path_env());
}
fn execute_path(self, path: &String, args: &HashMap<String, String>) {
let command = self.command_regisrty.command_by(path);
if let Some(c) = command {
self.execute_command(c, args)
} else {
println!("command not found")
match args.command {
CommandArg::Build => build(),
CommandArg::Health => health(),
CommandArg::InstallReveal => install_reveal()
}
}
fn execute_command(&self, c: &Box<dyn Command>, args: &HashMap<String, String>) {
let result = c.execute(args);
match result {
Ok(_) => println!("successfully executed"),
Err(message) => println!("{message}")
}
pub fn new() -> Self {
Self {}
}
}