From ffd3dc8e022b70b9665847562ac36d0c3d849aae Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Sun, 10 Nov 2024 01:01:49 +0200 Subject: [PATCH] Improve config initialization + fix #50 --- src/cli/config.rs | 22 ++++++++++++++-------- src/cli/main.rs | 2 +- src/core/templates/mod.rs | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/cli/config.rs b/src/cli/config.rs index 6ddf90f..3a9ef7c 100644 --- a/src/cli/config.rs +++ b/src/cli/config.rs @@ -1,28 +1,28 @@ +use colored::Colorize; use spark::Keywords; use spark::Template; use std::collections::HashMap; use std::fs; +use std::path::Path; +use std::path::PathBuf; use toml::Value; #[derive(Debug, Clone)] pub struct Config { pub path: String, - pub templates_path: String, + pub templates_path: PathBuf, } impl Config { pub fn new(path: &str) -> Self { let config_path = shellexpand::tilde(path).to_string(); - let mut config_dir: Vec<&str> = path.split('/').collect(); + let config_dir = Path::new(path).parent().unwrap(); - config_dir.pop(); - - //NOTE: maybe templates path should be parsed from config.toml itself?? - let templates = config_dir.join("/") + "/templates/"; + let templates = Path::new(config_dir).join("templates"); Config { path: config_path, - templates_path: shellexpand::tilde(&templates).to_string(), + templates_path: templates, } } @@ -62,7 +62,7 @@ content = ''' ''' "# .replace("CONFIGPATH", &self.path) - .replace("TEMPLATES_PATH", &self.templates_path); + .replace("TEMPLATES_PATH", &self.templates_path.to_str().unwrap()); let template: Template = toml::from_str(&conf_template).unwrap(); @@ -84,7 +84,13 @@ content = ''' keywords.insert(Keywords::from(key.to_string(), None), value_str); } } else { + println!( + "\n[{}] {}\n", + "INFO".bold().blue(), + "Looks like it's your first time running spark, creating config files and templates for you".green() + ); Self::init(self, keywords.clone()); + println!(""); } keywords diff --git a/src/cli/main.rs b/src/cli/main.rs index 07b5234..49151f9 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -34,7 +34,7 @@ fn main() { } let full_template_path = if fs::read_to_string(&template).is_err() { - format!("{}{}", config.templates_path, template) + format!("{}{}", config.templates_path.to_string_lossy(), template) } else { template }; diff --git a/src/core/templates/mod.rs b/src/core/templates/mod.rs index 3a3e96a..fe50a26 100644 --- a/src/core/templates/mod.rs +++ b/src/core/templates/mod.rs @@ -88,7 +88,7 @@ impl Template { let mut output = String::from(""); let re = Regex::new(KEYWORDS_REGEX).unwrap(); let files = self.files.clone().expect("No files table"); - let mut options = self.dump_options().expect("No options"); + let mut options = self.dump_options().unwrap_or_default(); files.into_iter().for_each(|file| { *keywords = Fns::find_and_exec(