wireguard_exporter/src/options.rs

70 lines
2.1 KiB
Rust
Raw Normal View History

2019-04-23 21:06:35 +00:00
#[derive(Debug, Clone)]
pub(crate) struct Options {
pub verbose: bool,
2019-07-11 13:31:25 +00:00
pub separate_allowed_ips: bool,
2019-05-17 17:32:35 +00:00
pub extract_names_config_file: Option<String>,
pub export_remote_ip_and_port: bool,
2019-04-23 21:06:35 +00:00
}
impl Options {
pub fn from_claps(matches: &clap::ArgMatches<'_>) -> Options {
2019-05-17 17:32:35 +00:00
if let Some(e) = matches.value_of("extract_names_config_file") {
Options {
verbose: matches.is_present("verbose"),
2019-07-11 13:31:25 +00:00
separate_allowed_ips: matches.is_present("separate_allowed_ips"),
2019-05-17 17:32:35 +00:00
extract_names_config_file: Some(e.to_owned()),
export_remote_ip_and_port: matches.is_present("export_remote_ip_and_port"),
2019-05-17 17:32:35 +00:00
}
} else {
Options {
verbose: matches.is_present("verbose"),
2019-07-11 13:31:25 +00:00
separate_allowed_ips: matches.is_present("separate_allowed_ips"),
2019-05-17 17:32:35 +00:00
extract_names_config_file: None,
export_remote_ip_and_port: matches.is_present("export_remote_ip_and_port"),
2019-05-17 17:32:35 +00:00
}
2019-04-23 21:06:35 +00:00
}
}
pub fn get_interface(&self) -> Option<&str> {
if let Some(config_file) = &self.extract_names_config_file {
let path = std::path::Path::new(config_file);
if let Some(file_stem) = path.file_stem() {
file_stem.to_str()
} else {
None
}
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_interface_some() {
let options = Options {
verbose: true,
separate_allowed_ips: false,
extract_names_config_file: Some("/etc/wireguard/wg0.conf".to_owned()),
export_remote_ip_and_port: true,
};
assert_eq!(options.get_interface(), Some("wg0"));
}
#[test]
fn test_interface_none() {
let options = Options {
verbose: true,
separate_allowed_ips: false,
extract_names_config_file: None,
export_remote_ip_and_port: true,
};
assert_eq!(options.get_interface(), None);
}
2019-04-23 21:06:35 +00:00
}