From b5bcc5526d18a6f1be3865d2fcebee2462aa2e5c Mon Sep 17 00:00:00 2001
From: Fontrodona Nicolas <nicolas.fontrodona@ens-lyon.fr>
Date: Wed, 1 Feb 2023 11:37:51 +0100
Subject: [PATCH] src/configt.rs: add gblk_project_name section in config

---
 src/configt.rs | 46 +++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 39 insertions(+), 7 deletions(-)

diff --git a/src/configt.rs b/src/configt.rs
index afeb4d1..c3221f4 100644
--- a/src/configt.rs
+++ b/src/configt.rs
@@ -77,12 +77,12 @@ pub fn get_global_config() -> Option<PathBuf> {
 ///   only the local one
 /// # Return
 /// The path containing the local of global configuration
-fn get_config(global: bool) -> PathBuf {
+pub(crate) fn get_config(global: bool) -> PathBuf {
     if global {
         match get_global_config() {
             Some(path) => path,
             None => {
-                println!("{} config not found !", "global".red());
+                eprintln!("{} config not found !", "global".red());
                 exit(0);
             }
         }
@@ -90,7 +90,7 @@ fn get_config(global: bool) -> PathBuf {
         match get_borgconfig() {
             Some(path) => path,
             None => {
-                println!("{} config not found !", "local".yellow());
+                eprintln!("{} config not found !", "local".red());
                 exit(0);
             }
         }
@@ -117,12 +117,18 @@ pub fn parse_toml(global: bool) -> Config {
         Ok(s) => s,
     };
     if content.is_empty() {
-        return Config {
-            gblk_prune: None,
-            gblk_remote: None,
-        };
+        if global {
+            return Config::new(global);
+        } else {
+            eprintln!(
+                "{}: Loccal config file should not be empty !",
+                "error".red()
+            );
+            exit(1);
+        }
     }
     let config_str: Config = toml::from_str(&content).unwrap();
+    config_str.check_project_type(global, &config_file);
     config_str
 }
 
@@ -346,6 +352,20 @@ pub fn write_config(config: &Config, global: bool) {
     };
 }
 
+/// Create a local config file
+pub fn create_local_config() {
+    let conf = Config::new(false);
+    write_config(&conf, false);
+}
+
+/// Create a named local config file
+/// # Arguments
+/// - `name`: The name of the config file
+pub fn create_named_local_config(name: &str) {
+    let conf = Config::new_named(name);
+    write_config(&conf, false);
+}
+
 /// Function that update the current gblk config file for the project folder
 /// # Arguments
 /// - `key` : The key to update
@@ -404,6 +424,18 @@ pub fn remove_config(key: &str, global: bool) -> () {
     };
 }
 
+/// Get the name of the current project
+pub(crate) fn get_project_name() -> String {
+    let config = parse_toml(false);
+    match config.get_name() {
+        None => {
+            eprintln!("{}: Unable to get the project name !", "error".red());
+            exit(1);
+        }
+        Some(name) => name,
+    }
+}
+
 /// Function that recover the local or global gblk config
 /// # Description
 /// This function return the parsed local gblk config file if it exists. If
-- 
GitLab