This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
forked from brndnmtthws/labhub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.rs
124 lines (106 loc) · 3.1 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use crate::commands;
use log::info;
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::sync::Mutex;
use toml;
use yansi::Paint;
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum Feature {
ExternalPr,
Commands,
}
#[derive(Debug, Deserialize)]
pub struct Config {
pub github: Site,
pub gitlab: Site,
pub mappings: Vec<Mapping>,
pub features: Vec<Feature>,
pub commands: Commands,
pub actions: Actions,
}
pub fn feature_enabled(feature: &Feature) -> bool {
CONFIG.features.contains(&feature)
}
pub fn command_enabled(command: &commands::CommandAction) -> bool {
feature_enabled(&Feature::Commands) && CONFIG.commands.enabled_commands.contains(&command)
}
pub fn action_enabled(action: &str) -> bool {
CONFIG.actions.enabled_actions.contains(&action.to_string())
}
#[derive(Debug, Deserialize)]
pub struct Actions {
pub enabled_actions: Vec<String>,
}
#[derive(Debug, Deserialize)]
pub struct Commands {
pub enabled_commands: Vec<commands::CommandAction>,
}
#[derive(Debug, Deserialize)]
pub struct Site {
pub webhook_secret: String,
pub username: String,
pub ssh_key: String,
pub api_token: String,
pub hostname: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct Mapping {
pub github_repo: String,
pub gitlab_repo: String,
}
lazy_static! {
pub static ref HUB_TO_LAB: Mutex<HashMap<String, String>> = {
let m: HashMap<String, String> = HashMap::new();
Mutex::new(m)
};
}
lazy_static! {
pub static ref LAB_TO_HUB: Mutex<HashMap<String, String>> = {
let m: HashMap<String, String> = HashMap::new();
Mutex::new(m)
};
}
fn get_labhub_toml_path() -> String {
env::var("LABHUB_TOML").unwrap_or_else(|_| "LabHub.toml".to_string())
}
lazy_static! {
pub static ref CONFIG: Config = {
let labhub_toml_path = get_labhub_toml_path();
let config: Config = toml::from_str(&read_file_to_string(&labhub_toml_path)).unwrap();
config
};
}
fn read_file_to_string(filename: &str) -> String {
let mut file = File::open(filename).expect("Unable to open the file");
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Unable to read the file");
contents
}
pub fn load_config() {
info!(
"Loaded LabHub configuration values from {}",
get_labhub_toml_path()
);
info!("CONFIG => {:#?}", Paint::red(&*CONFIG));
for mapping in CONFIG.mappings.iter() {
let mut hub_to_lab_lock = HUB_TO_LAB.lock();
let hub_to_lab = hub_to_lab_lock.as_mut().unwrap();
hub_to_lab.insert(mapping.github_repo.clone(), mapping.gitlab_repo.clone());
let mut lab_to_hub_lock = LAB_TO_HUB.lock();
let lab_to_hub = lab_to_hub_lock.as_mut().unwrap();
lab_to_hub.insert(mapping.gitlab_repo.clone(), mapping.github_repo.clone());
}
info!(
"HUB_TO_LAB => {:#?}",
Paint::red(HUB_TO_LAB.lock().unwrap())
);
info!(
"LAB_TO_HUB => {:#?}",
Paint::red(LAB_TO_HUB.lock().unwrap())
);
}