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

src/config_structure.rs: add gblk_project_name section in config

parent b5bcc552
Branches
No related tags found
No related merge requests found
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"),
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment