mod parser_configuration {
use std::collections::HashMap;
use std::env;
use std::fs;
use std::str::Lines;
pub struct VaultConfiguration {
pub map: HashMap<String, String>,
}
impl VaultConfiguration {
pub fn new(name_key_path: &str) -> Self {
let map = match env::var(&name_key_path) {
Ok(path) => get_map(&path),
Err(_) => HashMap::new(),
};
Self { map }
}
pub fn get_val(&self, name: &str) -> String {
match self.map.get(name) {
Some(value) => value.to_string(),
None => env::var(name).unwrap().to_string(),
}
}
}
fn get_map(path: &str) -> HashMap<String, String> {
parse_text_to_map(fs::read_to_string(path).unwrap().lines())
}
fn parse_text_to_map(array_text: Lines) -> HashMap<String, String> {
array_text
.map(|l| l.split(": ").collect::<Vec<&str>>())
.map(|x| (x[0].to_string(), x[1].to_string()))
.collect()
}
}