From 5b983d5e346aba136de50da0bfb052dc0e2ba5b2 Mon Sep 17 00:00:00 2001 From: Francesco Cogno Date: Fri, 17 May 2019 19:32:35 +0200 Subject: [PATCH] adding switch --- src/main.rs | 41 ++++++++++++++++++++++++++++++++--------- src/options.rs | 13 +++++++++++-- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 05cafb6..0583842 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,10 +63,13 @@ fn handle_request( fn perform_request( _req: Request, - _options: &Options, + options: &Options, ) -> impl Future, Error = ExporterError> { trace!("perform_request"); + // this is needed to satisfy the borrow checker + let options = options.clone(); + done( Command::new("wg") .arg("show") @@ -76,14 +79,29 @@ fn perform_request( ) .from_err() .and_then(|output| { - done(String::from_utf8(output.stdout)) - .from_err() - .and_then(|output_str| { - trace!("{}", output_str); - done(WireGuard::try_from(&output_str as &str)) + if let Some(extract_names_config_file) = options.extract_names_config_file { + Either::A( + done(String::from_utf8(output.stdout)) .from_err() - .and_then(|wg| ok(Response::new(Body::from(wg.render())))) - }) + .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())))) + }), + ) + } 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); diff --git a/src/options.rs b/src/options.rs index b24488e..4c39713 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,12 +1,21 @@ #[derive(Debug, Clone)] pub(crate) struct Options { pub verbose: bool, + pub extract_names_config_file: Option, } impl Options { pub fn from_claps(matches: &clap::ArgMatches<'_>) -> Options { - Options { - verbose: matches.is_present("verbose"), + 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, + } } } }