diff --git a/src/config_structure.rs b/src/config_structure.rs index a54002f5f441e545e7cf57ae2b346657bb977a4e..c65caaa4f79227efe361817b542fc49da3268719 100644 --- a/src/config_structure.rs +++ b/src/config_structure.rs @@ -1,14 +1,60 @@ +use std::{path::PathBuf, process::exit}; + use colored::Colorize; /// This module contains the structure of the gblk config file use serde_derive::{Deserialize, Serialize}; +use crate::commit; + /// /// Structure containing the global definition of the borg config file #[derive(Debug, Deserialize, Serialize, Clone)] pub struct Config { + pub gblk_project_name: GblkName, pub gblk_prune: Option<GblkConfig>, pub gblk_remote: Option<Vec<RemoteConfig>>, } +impl Config { + pub fn new(global: bool) -> Config { + Config { + gblk_project_name: GblkName::new(global), + gblk_prune: None, + gblk_remote: None, + } + } + pub fn new_named(name: &str) -> Config { + Config { + gblk_project_name: GblkName::new_named(name), + gblk_prune: None, + gblk_remote: None, + } + } + pub fn get_name(&self) -> Option<String> { + self.gblk_project_name.name.clone() + } + pub fn check_project_type(&self, is_global: bool, path: &PathBuf) -> () { + if is_global { + if &self.gblk_project_name.config_type != "global" { + eprintln!( + "{}: The type of the config file {} is not global", + "error".red(), + path.display() + ); + exit(1); + } + } else { + if &self.gblk_project_name.config_type != "local" { + eprintln!( + "{}: The type of the config file {} is not local", + "error".red(), + path.display() + ); + exit(1); + } + } + } +} + /// Structure corresponding to the gblk prune config to automatically use /// inside a project #[derive(Debug, Deserialize, Serialize, Clone)] @@ -81,3 +127,42 @@ impl RemoteConfig { } } } + +/// Structure that store the current borg name for the project +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct GblkName { + pub name: Option<String>, + pub config_type: String, +} + +impl GblkName { + pub fn new(global: bool) -> GblkName { + if global { + GblkName { + name: None, + config_type: String::from("global"), + } + } else { + let (borg_folder, _) = commit::check_path(); + let borg_folder = borg_folder.canonicalize().unwrap(); + let name = borg_folder + .parent() + .unwrap() + .file_stem() + .unwrap() + .to_str() + .unwrap() + .to_string(); + GblkName { + name: Some(name), + config_type: String::from("local"), + } + } + } + pub fn new_named(name: &str) -> GblkName { + GblkName { + name: Some(String::from(name)), + config_type: String::from("local"), + } + } +}