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

src/remote.rs: add update_config and remove_config function

parent a5401655
No related branches found
No related tags found
No related merge requests found
use crate::config_structure::{Config, RemoteConfig};
use crate::configt::{get_borgconfig, get_global_config, parse_toml};
use crate::configt::{get_borgconfig, get_global_config, write_config};
use colored::Colorize;
use regex::Regex;
use std::collections::BTreeMap as Dict;
use std::path::PathBuf;
......@@ -30,7 +31,7 @@ pub fn parse_config(global: bool) -> Config {
return Config {
repository: None,
gblk_prune: None,
remote: None,
gblk_remote: None,
};
}
Some(c) => c,
......@@ -50,7 +51,7 @@ pub fn parse_config(global: bool) -> Config {
return Config {
repository: None,
gblk_prune: None,
remote: None,
gblk_remote: None,
};
}
let re = Regex::new("id = (?P<first>[0-9a-z]+)\\W").unwrap();
......@@ -97,12 +98,75 @@ fn combine_remote(
dic
}
/// Get the remote config of a configuration file
/// # Arguments
/// - global: a boolean indicating to returns the remotes the the global or
/// local configuration file
/// # Return
/// Return the remotes of the local or the global configuration file
fn get_remotes(global: bool) -> Option<Vec<RemoteConfig>> {
let my_config = parse_config(global);
my_config.gblk_remote
}
/// Function that show the current gblk config of the borg folder
pub fn show() -> () {
let global_config = parse_config(true);
let local_config = parse_config(false);
let remote_global = global_config.remote;
let remote_local = local_config.remote;
let remote_global = get_remotes(true);
let remote_local = get_remotes(false);
let dic_remote = combine_remote(remote_global, remote_local);
display_remotes(dic_remote);
}
/// Function that update the current gblk config file for the project folder
/// # Arguments
/// - `key` : The remote name 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 my_config = parse_config(global);
let mut remotes = match my_config.gblk_remote {
Some(r) => r,
None => Vec::new(),
};
let mut updated = false;
for r in remotes.iter_mut() {
if r.name == key {
r.url = value.to_owned();
updated = true;
}
}
if !updated {
remotes.push(RemoteConfig {
name: key.to_owned(),
url: value.to_owned(),
})
}
my_config.gblk_remote = Some(remotes);
write_config(&my_config, global);
}
/// Function that remove a given remote in the current gblk config file for the project folder
/// # Arguments
/// - `key` : The remote name to delete
/// - `global: A bolean indicating whether to remove the key in the global or
/// the local configuration file
pub fn remove_config(key: &str, global: bool) -> () {
let mut my_config = parse_config(global);
let mut remotes = match my_config.gblk_remote {
Some(r) => r,
None => Vec::new(),
};
let init_size = remotes.len();
remotes.retain(|x| &x.name != &key);
let end_size = remotes.len();
if init_size == end_size {
eprintln!("{}: The remote {} doesn't exists !", "error".red(), key);
exit(1);
}
my_config.gblk_remote = if remotes.len() > 0 {
Some(remotes)
} else {
None
};
write_config(&my_config, global);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment