adding switch

This commit is contained in:
Francesco Cogno 2019-05-17 19:32:35 +02:00
parent 70178cb8d6
commit 5b983d5e34
No known key found for this signature in database
GPG key ID: 20883E192428EA7A
2 changed files with 43 additions and 11 deletions

View file

@ -63,10 +63,13 @@ fn handle_request(
fn perform_request(
_req: Request<Body>,
_options: &Options,
options: &Options,
) -> impl Future<Item = Response<Body>, Error = ExporterError> {
trace!("perform_request");
// this is needed to satisfy the borrow checker
let options = options.clone();
done(
Command::new("wg")
.arg("show")
@ -76,6 +79,8 @@ fn perform_request(
)
.from_err()
.and_then(|output| {
if let Some(extract_names_config_file) = options.extract_names_config_file {
Either::A(
done(String::from_utf8(output.stdout))
.from_err()
.and_then(|output_str| {
@ -83,7 +88,20 @@ fn perform_request(
done(WireGuard::try_from(&output_str as &str))
.from_err()
.and_then(|wg| ok(Response::new(Body::from(wg.render()))))
})
}),
)
} else {
Either::B(
done(String::from_utf8(output.stdout))
.from_err()
.and_then(|output_str| {
trace!("{}", output_str);
done(WireGuard::try_from(&output_str as &str))
.from_err()
.and_then(|wg| ok(Response::new(Body::from(wg.render()))))
}),
)
}
})
}
@ -94,7 +112,7 @@ fn main() {
.arg(
Arg::with_name("port")
.short("p")
.help("exporter port (default 9576)")
.help("exporter port")
.default_value("9576")
.takes_value(true),
)
@ -104,6 +122,11 @@ fn main() {
.help("verbose logging")
.takes_value(false),
)
.arg(
Arg::with_name("extract_names_config_file")
.short("n")
.help("If set, the exporter will look in the specified WireGuard config file for peer names (must be in [Peer] definition and be a comment)")
.takes_value(true))
.get_matches();
let options = Options::from_claps(&matches);

View file

@ -1,12 +1,21 @@
#[derive(Debug, Clone)]
pub(crate) struct Options {
pub verbose: bool,
pub extract_names_config_file: Option<String>,
}
impl Options {
pub fn from_claps(matches: &clap::ArgMatches<'_>) -> Options {
if let Some(e) = matches.value_of("extract_names_config_file") {
Options {
verbose: matches.is_present("verbose"),
extract_names_config_file: Some(e.to_owned()),
}
} else {
Options {
verbose: matches.is_present("verbose"),
extract_names_config_file: None,
}
}
}
}