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>,
|
2019-07-31 13:24:52 +00:00
|
|
|
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()),
|
2019-07-31 13:24:52 +00:00
|
|
|
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,
|
2019-07-31 13:24:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2019-10-13 17:42:44 +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
|
|
|
}
|