Skip to content

Commit

Permalink
Refactor code and remove unused gitmodules field
Browse files Browse the repository at this point in the history
from config file
  • Loading branch information
xN4P4LM committed Nov 3, 2023
1 parent 88c2e0c commit a99dac0
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 54 deletions.
1 change: 0 additions & 1 deletion config.yaml.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
project_path: . # relative to this file . indicates the same directory
docker_compose: docker-compose.yaml
gitmodules: .gitmodules
project_name: tools
project_version:
major: 0
Expand Down
7 changes: 6 additions & 1 deletion src/controller/init/submodules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ use crate::CONFIG;
pub fn get_submodule_paths() -> Vec<(String, String, bool)> {
// read the config file and get the .gitmodules file path and root path
let base_path = CONFIG.project_path.clone();
let submodule_path = CONFIG.gitmodules.clone();
// try to get the path to .gitmodule file in the current directory
let submodule_path = std::fs::canonicalize(format!("{}/{}", base_path, ".gitmodules"))
.expect("Couldn't get path to .gitmodules file")
.to_str()
.unwrap()
.to_string();

// read the .gitmodules file and get the paths of the submodules
// example .gitmodules file:
Expand Down
80 changes: 61 additions & 19 deletions src/helpers/config/app_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,12 @@ pub fn get_config() -> AppConfigFile {
// Check if the configuration file already exists
if !config_path.exists() {
// If the file does not exist, create a default configuration
let version = Version {
major: 0,
minor: 1,
patch: 0,
pre: Prerelease::new("alpha").unwrap(),
build: semver::BuildMetadata::EMPTY,
}
.to_string();
let config = AppConfigFile {
project_path: current_dir.to_str().unwrap().to_string(),
docker_compose: "docker-compose.yaml".to_string(),
gitmodules: ".gitmodules".to_string(),
project_name: "project".to_string(),
project_version: version,
github_api_token: None,
};

// Write the default configuration to a YAML file
let config = AppConfigFile::default();

// Write the configuration to a YAML file
write_config(&config);

// Return the default configuration
// Return the configuration
config
} else {
// If the file exists, read the configuration from the file
Expand Down Expand Up @@ -89,3 +74,60 @@ pub fn write_config(config: &AppConfigFile) {
file.write_all(config_yaml.as_bytes())
.expect("Couldn't write config to file");
}

/// Create a default configuration file.
///
/// ## Arguments
/// - `config_file: &str`: The path to the configuration file.
///
/// ## Returns
/// - `AppConfigFile`: The default configuration.
pub fn create_config(config_file: &str) -> AppConfigFile {
// get current directory path
let current_dir = current_dir().expect("Couldn't get current directory");

// get the path to the configuration file
let config_path = append_path(&current_dir, config_file);

// If the file does not exist, create a default configuration
let version = Version {
major: 0,
minor: 1,
patch: 0,
pre: Prerelease::new("alpha").unwrap(),
build: semver::BuildMetadata::EMPTY,
}
.to_string();
let config = AppConfigFile {
project_path: current_dir.to_str().unwrap().to_string(),
docker_compose: "docker-compose.yaml".to_string(),
project_name: "project".to_string(),
project_version: version,
github_api_token: None,
};

// Write the default configuration to a YAML file
write_config(&config);

// Return the default configuration
config
}

/// Check if the configuration file exists.
///
/// ## Returns
/// - `bool`: `true` if the configuration file exists, `false` otherwise.
pub fn check_if_config_exists() -> bool {
// get current directory path
let current_dir = current_dir().expect("Couldn't get current directory");

// get the path to the configuration file
let config_path = append_path(&current_dir, "config.yaml");

// Check if the configuration file already exists
if !config_path.exists() {
false
} else {
true
}
}
40 changes: 40 additions & 0 deletions src/helpers/user_input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,43 @@ pub fn normalize_prompt(prompt: &str) -> String {
format!("{}: ", local_prompt)
}
}

/// ## ask_user_yes_or_no(prompt: &str) -> bool
/// This function asks the user a yes or no question and returns true if the user
/// answers yes and false if the user answers no
/// This function is case insensitive and normalizes the input to lowercase ASCII
/// This function will keep asking the user for input until they answer yes or no
///
/// ### Arguments
/// - prompt: &str - The prompt to display to the user
///
/// ### Returns
/// - bool - true if the user answers yes, false if the user answers no
pub fn ask_user_yes_or_no(prompt: &str) -> bool {
// create a new string to store the user input
let mut input = String::new();

// loop until the user answers yes or no
loop {
// get the user input
input = get_user_input(prompt, Some(true));

// check if the user input is yes or no
if input == "yes" || input == "no" {
// break out of the loop
break;
} else {
// print an error message
println!("Please enter yes or no");
}
}

// check if the user input is yes
if input == "yes" {
// return true
true
} else {
// return false
false
}
}
38 changes: 14 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,19 @@ pub static CONFIG: Lazy<AppConfigFile> = Lazy::new(config::app_config::get_confi
/// ## main()
/// This function is the entry point for the program
fn main() {
// if the config project path is not the current directory,
// warn the user that this may cause unexpected behavior
if *CONFIG.project_path
!= *std::env::current_dir()
.expect("Couldn't get current directory")
.to_str()
.unwrap()
{
println!("WARNING: The project path in the configuration file is not the current directory. This may cause unexpected behavior.");
}

// switch statement based on the first argument and pass the rest of the arguments to the controller
match std::env::args().nth(1) {
Some(command) => {
let args: Vec<String> = std::env::args().collect();
if args.get(1).unwrap() == "help" {
helpers::help::router(&args[2..]);
return;
}
controller::run(command, &args[2..]);
}
None => {
helpers::help::main_help::print_help();
}
}
// // switch statement based on the first argument and pass the rest of the arguments to the controller
// match std::env::args().nth(1) {
// Some(command) => {
// let args: Vec<String> = std::env::args().collect();
// if args.get(1).unwrap() == "help" {
// helpers::help::router(&args[2..]);
// return;
// }
// controller::run(command, &args[2..]);
// }
// None => {
// helpers::help::main_help::print_help();
// }
// }
}
12 changes: 3 additions & 9 deletions src/models/app_config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use semver::{BuildMetadata, Prerelease, Version};
use serde::{Deserialize, Serialize};
use std::{error::Error, fs};
use std::error::Error;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct AppConfigFile {
pub project_path: String,
pub docker_compose: String,
pub gitmodules: String,
pub project_name: String,
pub project_version: String,
pub github_api_token: Option<String>,
Expand Down Expand Up @@ -78,7 +77,6 @@ fn test_app_config() {
let config = AppConfigFile {
project_path: ".".to_string(),
docker_compose: "docker-compose.yaml".to_string(),
gitmodules: ".gitmodules".to_string(),
project_name: "project".to_string(),
project_version: Version {
major: 0,
Expand All @@ -94,7 +92,6 @@ fn test_app_config() {
let config_yaml = r#"---
project_path: .
docker_compose: docker-compose.yaml
gitmodules: .gitmodules
project_name: project
project_version: 0.1.0
github_api_token: null
Expand All @@ -109,7 +106,6 @@ fn test_app_config_update_version() {
let mut config = AppConfigFile {
project_path: ".".to_string(),
docker_compose: "docker-compose.yaml".to_string(),
gitmodules: ".gitmodules".to_string(),
project_name: "project".to_string(),
project_version: Version {
major: 0,
Expand All @@ -125,7 +121,6 @@ fn test_app_config_update_version() {
let config_yaml = r#"---
project_path: .
docker_compose: docker-compose.yaml
gitmodules: .gitmodules
project_name: project
project_version: 0.1.0
github_api_token: null
Expand All @@ -139,7 +134,6 @@ github_api_token: null
let config_yaml = r#"---
project_path: .
docker_compose: docker-compose.yaml
gitmodules: .gitmodules
project_name: project
project_version: 0.2.0
github_api_token: null
Expand All @@ -154,15 +148,15 @@ fn test_app_config_read_from_file() {
let config_yaml = r#"---
project_path: . # relative to this file . indicates the same directory
docker_compose: docker-compose.yaml
gitmodules: .gitmodules
project_name: tools
project_version: 0.0.1
github_token: gh_token # if you want to use github api instead of the GitHub CLI
"#;

let config = AppConfigFile::from_yaml(config_yaml).unwrap();

let config_from_file = fs::read_to_string("src/test/models/app_config/config.yaml").unwrap();
let config_from_file =
std::fs::read_to_string("src/test/models/app_config/config.yaml").unwrap();

let from_file = AppConfigFile::from_yaml(&config_from_file);
match from_file {
Expand Down

0 comments on commit a99dac0

Please sign in to comment.