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