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

add global configuration for pruning

parent fbebc4cd
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ use crate::commit; ...@@ -2,6 +2,7 @@ use crate::commit;
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;
use home;
use regex::Regex; use regex::Regex;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use std::{path::PathBuf, process::exit}; use std::{path::PathBuf, process::exit};
...@@ -51,7 +52,7 @@ pub(crate) struct PartialPrune { ...@@ -51,7 +52,7 @@ pub(crate) struct PartialPrune {
/// Structure containing the global definition of the borg config file /// Structure containing the global definition of the borg config file
#[derive(Debug, Deserialize, Serialize, Clone)] #[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Config { pub struct Config {
repository: Repository, repository: Option<Repository>,
gblk_prune: Option<GblkConfig>, gblk_prune: Option<GblkConfig>,
} }
...@@ -139,16 +140,60 @@ fn get_borgconfig() -> PathBuf { ...@@ -139,16 +140,60 @@ fn get_borgconfig() -> PathBuf {
config_path config_path
} }
fn parse_toml() -> Config { /// Get the global configuration for gblk
let config_file = get_borgconfig(); /// # 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);
}
};
global_config.push(".gblkconfig");
global_config
}
/// Get the configuration file of interest
/// # Argument
/// - global: A bolean indicating whether to get the global configuration or
/// only the local one
/// # Return
/// The path containing the local of global configuration
fn get_config(global: bool) -> PathBuf {
if global {
get_global_config()
} else {
get_borgconfig()
}
}
/// Return the parsed config of interest
/// # Arguments
/// - global: A bolean indicating whether to get the global configuration or
/// only the local one
/// # Return
/// The parsed toml file
fn parse_toml(global: bool) -> Config {
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 {
Err(e) => { Err(e) => match e.raw_os_error() {
eprintln!("Unable to read {} - {}", &config_file.to_str().unwrap(), e); Some(2) => String::from(""),
exit(101); _ => {
} eprintln!("Unable to read {} - {}", &config_file.to_str().unwrap(), e);
exit(101);
}
},
Ok(s) => s, Ok(s) => s,
}; };
if content.is_empty() {
return Config {
repository: None,
gblk_prune: 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();
let nc = re.replace(&content, "id = '$first'"); let nc = re.replace(&content, "id = '$first'");
let config_str: Config = toml::from_str(&nc).unwrap(); let config_str: Config = toml::from_str(&nc).unwrap();
...@@ -156,13 +201,22 @@ fn parse_toml() -> Config { ...@@ -156,13 +201,22 @@ fn parse_toml() -> Config {
} }
/// Function that show the current gblk config of the borg folder /// Function that show the current gblk config of the borg folder
pub fn show() -> () { /// # Arguments
let config = parse_toml(); /// - global: A bolean indicating if we whant to show the config of the current
/// profile or the global configuration
pub fn show(global: bool) -> () {
let config = parse_toml(global);
let kind = if global { "global" } else { "local" };
let gblk_config: GblkConfig = match config.gblk_prune { let gblk_config: GblkConfig = match config.gblk_prune {
Some(data) => data, Some(data) => data,
None => { None => {
println!("{}", "No configuration defined for this project".yellow()); println!(
exit(0) "{} {} {}",
"No".yellow(),
kind.green(),
"configuration defined".yellow()
);
exit(0);
} }
}; };
let gblk_str = toml::to_string_pretty(&gblk_config).unwrap(); let gblk_str = toml::to_string_pretty(&gblk_config).unwrap();
...@@ -347,19 +401,23 @@ fn toml_to_string(config: &Config) -> String { ...@@ -347,19 +401,23 @@ fn toml_to_string(config: &Config) -> String {
/// # Arguments /// # Arguments
/// - `key` : The key to update /// - `key` : The key to update
/// - `value`: The value of the key to update /// - `value`: The value of the key to update
pub fn update_config(key: &str, value: &str) -> () { /// - `global: A bolean indicating whether to update the global or the local configuration
let mut config = parse_toml(); pub fn update_config(key: &str, value: &str, global: bool) -> () {
let mut config = parse_toml(global);
let mut gblk_config: GblkConfig = get_gblkconfig(&config); let mut gblk_config: GblkConfig = get_gblkconfig(&config);
update_val(&mut gblk_config, key, value); update_val(&mut gblk_config, key, value);
config.gblk_prune = Some(gblk_config); config.gblk_prune = Some(gblk_config);
let gblk_str = toml_to_string(&config); let gblk_str = toml_to_string(&config);
let config_file = get_borgconfig(); let config_file = get_config(global);
let kind = if global { "global" } else { "local" };
match std::fs::write(&config_file, gblk_str) { match std::fs::write(&config_file, gblk_str) {
Err(_) => { Err(_) => {
eprintln!( eprintln!(
"{} {} {} {}", "{} {} {} {} {} {}",
"error:".red(), "error:".red(),
"Unable to write the config file", "Unable to write the",
kind.green(),
"config file",
config_file.to_str().unwrap(), config_file.to_str().unwrap(),
"!" "!"
); );
...@@ -373,8 +431,10 @@ pub fn update_config(key: &str, value: &str) -> () { ...@@ -373,8 +431,10 @@ pub fn update_config(key: &str, value: &str) -> () {
/// for a given arguments /// for a given arguments
/// # Arguments /// # Arguments
/// - `key` : The key to update /// - `key` : The key to update
pub fn remove_config(key: &str) -> () { /// - `global`: A bolean indicating whether to remove a global or a local
let mut config = parse_toml(); /// configuration file
pub fn remove_config(key: &str, global: bool) -> () {
let mut config = parse_toml(global);
let mut gblk_config: GblkConfig = get_gblkconfig(&config); let mut gblk_config: GblkConfig = get_gblkconfig(&config);
remove_val(&mut gblk_config, key); remove_val(&mut gblk_config, key);
config.gblk_prune = if gblk_config.empty() { config.gblk_prune = if gblk_config.empty() {
...@@ -383,13 +443,16 @@ pub fn remove_config(key: &str) -> () { ...@@ -383,13 +443,16 @@ pub fn remove_config(key: &str) -> () {
Some(gblk_config) Some(gblk_config)
}; };
let gblk_str = toml_to_string(&config); let gblk_str = toml_to_string(&config);
let config_file = get_borgconfig(); let config_file = get_config(global);
let kind = if global { "global" } else { "local" };
match std::fs::write(&config_file, gblk_str) { match std::fs::write(&config_file, gblk_str) {
Err(_) => { Err(_) => {
eprintln!( eprintln!(
"{} {} {} {}", "{} {} {} {} {} {}",
"error:".red(), "error:".red(),
"Unable to write the config file", "Unable to write the",
kind.green(),
"config file",
config_file.to_str().unwrap(), config_file.to_str().unwrap(),
"!" "!"
); );
...@@ -399,16 +462,31 @@ pub fn remove_config(key: &str) -> () { ...@@ -399,16 +462,31 @@ pub fn remove_config(key: &str) -> () {
}; };
} }
/// Function that recover the local or global gblk config
/// # Description
/// This function return the parsed local gblk config file if it exists. If
/// this config is not defined, then it will return tje global gblk config file
fn parse_local_or_global_config() -> GblkConfig {
let config = parse_toml(false);
let gblk_config: GblkConfig = get_gblkconfig(&config);
if gblk_config.empty() {
let config = parse_toml(true);
let gblk_config: GblkConfig = get_gblkconfig(&config);
return gblk_config;
}
gblk_config
}
/// Execute a prune based on gblk configuration /// Execute a prune based on gblk configuration
/// # Arguments /// # Arguments
/// - config_prune: contains partial configuration for prune that cannot be /// - `config_prune`: contains partial configuration for prune that cannot be
/// used in the configuration file /// used in the configuration file
/// - `global`: a boolean indicating whether to prune using
pub(crate) fn launch_config_prune(config_prune: PartialPrune) { pub(crate) fn launch_config_prune(config_prune: PartialPrune) {
let config = parse_toml(); let gblk_config = parse_local_or_global_config();
let gblk_config: GblkConfig = get_gblkconfig(&config);
if gblk_config.empty() { if gblk_config.empty() {
eprintln!( eprintln!(
"{} No configuration defined for pruning!", "{} No global and local configuration defined for pruning!",
"error:".red() "error:".red()
); );
exit(104); exit(104);
......
...@@ -220,7 +220,7 @@ enum Config { ...@@ -220,7 +220,7 @@ enum Config {
/// prune some commits /// prune some commits
Add(Add), Add(Add),
/// Display the current gblk configuration /// Display the current gblk configuration
Show, Show(Show),
/// Remove the configuration of a given key in prune /// Remove the configuration of a given key in prune
Rm(Rm), Rm(Rm),
/// Prune using the project configuration /// Prune using the project configuration
...@@ -233,12 +233,29 @@ struct Add { ...@@ -233,12 +233,29 @@ struct Add {
key: String, key: String,
/// The value of the argument to add in the configuration file /// The value of the argument to add in the configuration file
value: String, value: String,
/// Use this flag if you wan to add an argument in the global gblk
/// configuration file
#[clap(short, long, takes_value = false)]
global: bool,
} }
#[derive(Debug, Args)] #[derive(Debug, Args)]
struct Rm { struct Rm {
/// The name of the argument to remove, see optional arguments of the prune subcommands /// The name of the argument to remove, see optional arguments of the prune subcommands
key: String, key: String,
/// Use this flag if you wan to remove an argument in the global gblk
/// configuration file
#[clap(short, long, takes_value = false)]
global: bool,
}
#[derive(Debug, Args)]
struct Show {
/// Use this flag if you wan to remove an argument in the global gblk
/// configuration file
#[clap(short, long, takes_value = false)]
global: bool,
} }
fn main() { fn main() {
...@@ -309,9 +326,9 @@ fn main() { ...@@ -309,9 +326,9 @@ fn main() {
compact::launch_compact(my_compact); compact::launch_compact(my_compact);
} }
Commands::Config(conf) => match conf { Commands::Config(conf) => match conf {
Config::Add(e) => configt::update_config(&e.key, &e.value), Config::Add(e) => configt::update_config(&e.key, &e.value, e.global),
Config::Show => configt::show(), Config::Show(e) => configt::show(e.global),
Config::Rm(e) => configt::remove_config(&e.key), Config::Rm(e) => configt::remove_config(&e.key, e.global),
Config::Prune(pp) => configt::launch_config_prune(pp), Config::Prune(pp) => configt::launch_config_prune(pp),
}, },
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment