use std::ops::Deref; use camino::{Utf8Path, Utf8PathBuf}; use scurvy::{ Argument, Scurvy, arg, formula::{BooleanFormula, U8Formula, Utf8PathFormula}, }; #[derive(Clone, Debug)] pub struct Config { socket_path: Utf8PathBuf, database_path: Utf8PathBuf, network_interface: String, graph_path: Utf8PathBuf, graph_update: u8, graph_on_boot: bool, } impl Config { pub fn get() -> Self { let args = vec![ arg!(["config", "c"] = "path"), arg!(["socket", "s"] = "path"), arg!(["database", "db"] = "path"), arg!("net" = "interface"), arg!("graph-directory" = "path"), arg!("graph-update" = "minutes"), arg!("graph-on-boot" = "bool"), ]; let scurvy = Scurvy::make(args).with_configuration_key("config", "/etc/gatherer.conf"); let socket_path = scurvy.parse_req("socket", Utf8PathFormula); let database_path = scurvy.parse_req("database", Utf8PathFormula); let interface = scurvy.get_req(["net", "NetworkInterface"]).to_owned(); let graph_path = scurvy.parse_req(["graph-directory", "GraphDirectory"], Utf8PathFormula); let graph_update = scurvy.parse_req( ["graph-update", "GraphUpdate"], U8Formula::new().bounds(1..=60), ); let graph_on_boot = scurvy .parse(["graph-on-boot", "GraphOnBoot"], BooleanFormula) .map(|b| *b.deref()) .unwrap_or(false); Self { socket_path, database_path, network_interface: interface, graph_path, graph_update, graph_on_boot, } } pub fn socket_path(&self) -> &Utf8Path { &self.socket_path } pub fn database_path(&self) -> &Utf8Path { &self.database_path } pub fn network_interface(&self) -> &str { &self.network_interface } pub fn graph_path(&self) -> &Utf8Path { &self.graph_path } pub fn graph_update(&self) -> u8 { self.graph_update } pub fn graph_on_boot(&self) -> bool { self.graph_on_boot } }