Skip to content

Commit

Permalink
Update main.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnxpl0it committed Oct 13, 2024
1 parent 9cd7c4c commit 31df69e
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
use idkmng::Config;
use crate::config::Config;
use idkmng::Keywords;
use idkmng::Template;
use std::fs;
mod args;
use args::Cli;
mod config;
use colored::*;

fn main() {
let args = Cli::parse();
let config = Config::new(args.value_of("config").unwrap());
let mut keywords = Keywords::init(config.clone());
let mut json_data: serde_json::Value = Default::default();
let mut keywords = Keywords::init();

if args.is_present("json") {
let json_file = fs::read_to_string(args.value_of("json").unwrap());
json_data = serde_json::from_str(&json_file.unwrap()).unwrap();
}
keywords.extend(config.clone().get_keywords());

if args.subcommand_matches("init").is_some() {
let dest = format!(
Expand All @@ -29,14 +26,53 @@ fn main() {
);
println!("{}: {}", "Creating Template".bold().green(), &dest.yellow());
Template::generate(&dest);
} else if let Some(filename) = args.value_of("template") {
let template = Template::validate(filename.to_string(), config.templates_path.clone());
println!("\n{}: {}", "Using Template".blue(), &template.magenta());
} else if let Some(temp) = args.value_of("template") {
let mut template = temp.to_string();

if !template.ends_with(".toml") {
template += ".toml";
}

let full_template_path = if fs::read_to_string(&template).is_err() {
format!("{}{}", config.templates_path, template)
} else {
template
};

let template_content = fs::read_to_string(&full_template_path).unwrap_or_else(|_| {
panic!(
"{}: {}",
"Failed to read template".red().bold(),
full_template_path
)
});

let mut parsed_template: Template = toml::from_str(&template_content).unwrap();

let mut options = parsed_template.dump_options().unwrap_or_default();

if !args.is_present("quiet") {
Template::show_info(&Template::parse(&template, true));
println!(
"\n{}: {}",
"Using Template".blue(),
full_template_path.magenta()
);
Template::show_info(&parsed_template);
}

if args.is_present("json") {
let json_file = fs::read_to_string(args.value_of("json").unwrap());
let json_data = serde_json::from_str(&json_file.unwrap()).unwrap();
options.set_json(json_data);
}

if args.is_present("git") {
options.set_git(true);
options.set_project_root("{{$PROJECTNAME}}");
}

Template::extract(template, true, &mut keywords, json_data);
parsed_template.set_options(options);
parsed_template.extract(&mut keywords);
} else {
println!(
"{} {}",
Expand Down

0 comments on commit 31df69e

Please sign in to comment.