Skip to content

Commit

Permalink
fix(config file): removed use of configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
veritem committed Nov 4, 2023
1 parent 8c14583 commit 4fd87ed
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 52 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ keywords = ["git", "commit", "conventional", "commits"]
clap = { version = "4.0", features = ["derive"] }
dialoguer = "0.11.0"
inquire = "0.6.2"
serde = "1.0.188"
serde = {version = "1.0.188", features = ["derive"]}
serde_yaml = "0.9.25"
yaml-rust = "0.4.5"
yaml-rust = "0.4.5"
25 changes: 23 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Parser;
mod cli;
mod utils;
use crate::utils::{create_and_or_read_config, validate_git_project};
use inquire::Confirm;
use std::process::Command;

#[derive(Parser, Debug)]
Expand All @@ -26,8 +27,28 @@ fn main() {
let is_project_valid = validate_git_project();

if let Some(error) = is_project_valid {
println!("{error}");
std::process::exit(0);
if error == "Some changes were not added to commit" {
// ask if they want to add to commit and do it
println!("\n");
println!("{error}");
println!("\n");
let confirm = Confirm::new("Do you want to add all changes to commit?")
.with_default(false)
.prompt();

if confirm.unwrap() {
let commit_output = Command::new("git")
.args(["add", "."])
.output()
.expect("failed to execute process");
println!("{}", String::from_utf8_lossy(&commit_output.stdout));
} else {
std::process::exit(0);
}
} else {
println!("{error}");
std::process::exit(0);
}
}

let single_line_commit: Option<String> =
Expand Down
53 changes: 5 additions & 48 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::fs::File;
use serde::Deserialize;
use std::collections::HashMap;
use std::process::Command;
use std::{collections::HashMap, fs, io::Write};
use yaml_rust::YamlLoader;

#[derive(Debug)]
#[derive(Debug, Deserialize)]
pub struct GCommitConfig {
pub classes: HashMap<String, String>,
pub scopes: Vec<String>,
Expand Down Expand Up @@ -66,40 +65,7 @@ pub fn build_commit_message(
}

pub fn create_and_or_read_config() -> GCommitConfig {
let mut config: GCommitConfig = GCommitConfig {
classes: HashMap::new(),
scopes: Vec::new(),
};

let config_file = fs::read_to_string(".gcommit.yml");

match config_file {
Ok(value) => {
let loaded_config = YamlLoader::load_from_str(&value).unwrap();
for k in loaded_config.iter() {
if k["classes"].as_hash().into_iter().len() == 0 {
panic!("No classes added in your .gcommit.yml file")
}

for (key, value) in k["classes"].as_hash().unwrap().iter() {
config.classes.insert(
key.clone().into_string().unwrap(),
value.clone().into_string().unwrap(),
);
}
if k["scopes"].as_vec().into_iter().len() == 0 {
panic!("No scopes available in your .gcommitconfig.ml file")
} else {
for scope in k["scopes"].as_vec().unwrap().iter() {
config.scopes.push(scope.clone().into_string().unwrap());
}
}
}
}
Err(_error) => {
println!("Found no .gcommit.yml, creating a default one...");
config = {
let default_config_file = r#"
let data = r#"
classes:
feat: "A new feature"
fix: "A bug fix"
Expand All @@ -114,16 +80,7 @@ scopes:
- docs
"#;

match File::create(".gcommit.yml") {
Ok(mut file) => &file.write_all(default_config_file.as_bytes()),
Err(error) => panic!("{:?}", error),
};

// re-read the file again to load the default configurations.
create_and_or_read_config()
}
}
}
let config: GCommitConfig = serde_yaml::from_str(data).unwrap();

config
}
Expand Down

0 comments on commit 4fd87ed

Please sign in to comment.