Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnxpl0it committed Oct 30, 2024
1 parent a2fbd69 commit 78a0c93
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 113 deletions.
5 changes: 4 additions & 1 deletion src/core/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ impl Keywords {
Self::from(String::from("HOME"), None),
env::var("HOME").unwrap(),
);
keywords.insert(Self::from(String::from("PROJECTNAME"), None), "".to_string());
keywords.insert(
Self::from(String::from("PROJECTNAME"), None),
"".to_string(),
);
keywords.insert(
Self::from(String::from("CURRENTDIR"), None),
env::current_dir()
Expand Down
9 changes: 1 addition & 8 deletions src/core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pub mod keywords;
mod options;
mod templates;
mod utils;
pub use options::Options;
use serde::{Deserialize, Serialize};

pub struct Keywords {}

#[derive(Debug, Deserialize, Serialize)]
Expand All @@ -15,13 +15,6 @@ pub struct Information {
pub description: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Options {
pub git: bool,
pub json_data: Option<serde_json::Value>,
pub project_root: String,
}

#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct File {
pub path: String,
Expand Down
92 changes: 0 additions & 92 deletions src/core/options.rs

This file was deleted.

48 changes: 48 additions & 0 deletions src/core/options/git/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use colored::*;
use std::process::Command;

pub fn check_git() -> Result<(), String> {
if Command::new("git").arg("--version").spawn().is_err() {
return Err("Git is not installed. Please install git and try again.".to_string());
}
Ok(())
}

pub fn git_init(project_root: &str) {
if let Err(e) = check_git() {
println!("{}: {}", "error".red().bold(), e.red().bold());
return;
}

if let Err(e) = std::env::set_current_dir(project_root) {
println!(
"{}: {}",
"error".red().bold(),
format!("Failed to change directory: {}", e).red().bold()
);
return;
}

let cmd = Command::new("git")
.arg("init") //TODO: maybe add git arguments? that can be a bit risky..
.stderr(std::process::Stdio::null()) // hide hints and errors
.status();

if let Ok(status) = cmd {
if status.success() {
println!("{}", "\n✅ Git initialized successfully.".green().bold());
} else {
println!(
"{}: {}",
"error".red().bold(),
"Git initialization failed.".red().bold()
);
}
} else {
println!(
"{}: {}",
"error".red().bold(),
"Failed to run git command.".red().bold()
);
}
}
10 changes: 10 additions & 0 deletions src/core/options/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};
pub mod git;
pub mod opts;

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Options {
pub git: bool,
pub json_data: Option<serde_json::Value>,
pub project_root: String,
}
47 changes: 47 additions & 0 deletions src/core/options/opts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::options::git;
use crate::Options;
use colored::*;

impl Default for Options {
fn default() -> Self {
Self {
json_data: Some(serde_json::Value::Null),
git: false,
project_root: String::new(),
}
}
}

impl Options {
pub fn set_git(&mut self, git: bool) {
self.git = git;
}

pub fn set_json(&mut self, json_data: serde_json::Value) {
self.json_data = Some(json_data);
}

pub fn set_project_root(&mut self, project_root: &str) {
self.project_root = project_root.to_string();
}

pub fn handle_options(self) {
if self.git {
if self.project_root.is_empty() {
println!(
"{}: {}",
"error".red().bold(),
"Please specify a project root.".red().bold()
);
return;
}

println!(
"\nInitializing git repository for {}\n",
self.project_root.blue()
);

git::git_init(&self.project_root);
}
}
}
22 changes: 10 additions & 12 deletions src/core/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ impl Template {
}

pub fn dump_options(&mut self) -> Option<Options> {
if self.options.is_none() {
return None;
}
self.options.as_ref()?;
Some(self.options.clone().unwrap())
}

Expand Down Expand Up @@ -97,16 +95,15 @@ impl Template {
options.json_data.clone().unwrap_or(serde_json::Value::Null),
);

if file.path.contains("{{$PROJECTNAME}}")
if (file.path.contains("{{$PROJECTNAME}}")
|| file.content.contains("{{$PROJECTNAME}}")
|| options.project_root.contains("{{$PROJECTNAME}}")
|| options.project_root.contains("{{$PROJECTNAME}}"))
&& project.is_empty()
{
if project.is_empty() {
project = prompt("Project name").unwrap();
keywords.insert("{{$PROJECTNAME}}".to_string(), project.to_owned());
if options.project_root == "{{$PROJECTNAME}}" {
options.set_project_root(&project);
}
project = prompt("Project name").unwrap();
keywords.insert("{{$PROJECTNAME}}".to_string(), project.to_owned());
if options.project_root == "{{$PROJECTNAME}}" {
options.set_project_root(&project);
}
}

Expand All @@ -127,7 +124,8 @@ impl Template {
write_content(&shellexpand::tilde(&path), liquified)
});

Options::handle_options(options);
//TODO: Move this to cli/main.rs
options.handle_options();
}

pub fn show_info(template: &Self) {
Expand Down

0 comments on commit 78a0c93

Please sign in to comment.