Skip to content
Snippets Groups Projects
Verified Commit a5401655 authored by nfontrod's avatar nfontrod
Browse files

src/configt.rs: minor change of some functions

parent f90d42fe
Branches
No related tags found
No related merge requests found
use crate::commit; use crate::commit;
use crate::config_structure::{Config, GblkConfig}; use crate::config_structure::{Config, GblkConfig, RemoteConfig};
use crate::prune::{new_prune, prune_launcher}; use crate::prune::{new_prune, prune_launcher};
use clap::Args; use clap::Args;
use colored::Colorize; use colored::Colorize;
...@@ -51,28 +51,27 @@ pub(crate) struct PartialPrune { ...@@ -51,28 +51,27 @@ pub(crate) struct PartialPrune {
/// # Return /// # Return
/// A pathbuf variable containing the location of borg config file for the /// A pathbuf variable containing the location of borg config file for the
/// current project /// current project
fn get_borgconfig() -> PathBuf { pub fn get_borgconfig() -> Option<PathBuf> {
let (mut config_path, _) = commit::check_path(); let (mut config_path, _) = commit::check_path();
config_path.push("config"); config_path.push("config");
if !config_path.is_file() { if !config_path.is_file() {
eprintln!("{} not found !", config_path.to_str().unwrap()); return None;
exit(1);
} }
config_path Some(config_path)
} }
/// Get the global configuration for gblk /// Get the global configuration for gblk
/// # Return /// # Return
/// - The path where the global location of the gblk configuration should be set /// - The path where the global location of the gblk configuration should be set
fn get_global_config() -> PathBuf { pub fn get_global_config() -> Option<PathBuf> {
let mut global_config = match home::home_dir() { let global_config = match home::home_dir() {
Some(path) => path, Some(path) => {
None => { let mut tmp = path;
eprintln!("{} global config not found!", "error:".red()); tmp.push(".gblkconfig");
exit(1); Some(tmp)
} }
None => None,
}; };
global_config.push(".gblkconfig");
global_config global_config
} }
...@@ -84,9 +83,21 @@ fn get_global_config() -> PathBuf { ...@@ -84,9 +83,21 @@ fn get_global_config() -> PathBuf {
/// The path containing the local of global configuration /// The path containing the local of global configuration
fn get_config(global: bool) -> PathBuf { fn get_config(global: bool) -> PathBuf {
if global { if global {
get_global_config() match get_global_config() {
Some(path) => path,
None => {
eprintln!("local config not found !");
exit(1);
}
}
} else { } else {
get_borgconfig() match get_borgconfig() {
Some(path) => path,
None => {
eprintln!("global config not found !");
exit(1);
}
}
} }
} }
...@@ -96,7 +107,7 @@ fn get_config(global: bool) -> PathBuf { ...@@ -96,7 +107,7 @@ fn get_config(global: bool) -> PathBuf {
/// only the local one /// only the local one
/// # Return /// # Return
/// The parsed toml file /// The parsed toml file
fn parse_toml(global: bool) -> Config { pub fn parse_toml(global: bool) -> Config {
let config_file = get_config(global); let config_file = get_config(global);
let rcontent = std::fs::read_to_string(&config_file); let rcontent = std::fs::read_to_string(&config_file);
let content = match rcontent { let content = match rcontent {
...@@ -113,6 +124,7 @@ fn parse_toml(global: bool) -> Config { ...@@ -113,6 +124,7 @@ fn parse_toml(global: bool) -> Config {
return Config { return Config {
repository: None, repository: None,
gblk_prune: None, gblk_prune: None,
gblk_remote: None,
}; };
} }
let re = Regex::new("id = (?P<first>[0-9a-z]+)\\W").unwrap(); let re = Regex::new("id = (?P<first>[0-9a-z]+)\\W").unwrap();
...@@ -318,16 +330,11 @@ fn toml_to_string(config: &Config) -> String { ...@@ -318,16 +330,11 @@ fn toml_to_string(config: &Config) -> String {
nc nc
} }
/// Function that update the current gblk config file for the project folder /// Function used to write a configuration file
/// # Arguments /// # Arguments
/// - `key` : The key to update /// - `config` The structure representing the configuration file to write
/// - `value`: The value of the key to update /// - `global`: A boolean indicating whether or not to write the configuration file
/// - `global: A bolean indicating whether to update the global or the local configuration pub fn write_config(config: &Config, global: bool) {
pub fn update_config(key: &str, value: &str, global: bool) -> () {
let mut config = parse_toml(global);
let mut gblk_config: GblkConfig = get_gblkconfig(&config);
update_val(&mut gblk_config, key, value);
config.gblk_prune = Some(gblk_config);
let gblk_str = toml_to_string(&config); let gblk_str = toml_to_string(&config);
let config_file = get_config(global); let config_file = get_config(global);
let kind = if global { "global" } else { "local" }; let kind = if global { "global" } else { "local" };
...@@ -348,6 +355,29 @@ pub fn update_config(key: &str, value: &str, global: bool) -> () { ...@@ -348,6 +355,29 @@ pub fn update_config(key: &str, value: &str, global: bool) -> () {
}; };
} }
/// Function that update the current gblk config file for the project folder
/// # Arguments
/// - `key` : The key to update
/// - `value`: The value of the key to update
/// - `global: A bolean indicating whether to update the global or the local configuration
pub fn update_config(key: &str, value: &str, global: bool) -> () {
let mut config = parse_toml(global);
let mut gblk_config: GblkConfig = get_gblkconfig(&config);
update_val(&mut gblk_config, key, value);
config.gblk_prune = Some(gblk_config);
config.gblk_remote = Some(vec![
RemoteConfig {
name: String::from("lol"),
url: String::from("test"),
},
RemoteConfig {
name: String::from("lol2"),
url: String::from("test2"),
},
]);
write_config(&config, global);
}
/// Function that remove the current gblk config file for the project folder /// Function that remove the current gblk config file for the project folder
/// for a given arguments /// for a given arguments
/// # Arguments /// # Arguments
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment