Merge pull request #45 from MindFlavor/issue/27/sudo_wg_optional

Added optional sudo to wg command
This commit is contained in:
Francesco Cogno 2020-10-11 20:38:56 +02:00 committed by GitHub
commit 38b55fca32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 539 additions and 528 deletions

1032
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
[package]
name = "prometheus_wireguard_exporter"
version = "3.4.0"
version = "3.4.1"
authors = ["Francesco Cogno <francesco.cogno@outlook.com>"]
description = "Prometheus WireGuard Exporter"
edition = "2018"

View file

@ -47,11 +47,21 @@ async fn perform_request(
let mut wg_accumulator: Option<WireGuard> = None;
for interface_to_handle in interfaces_to_handle {
let output = Command::new("wg")
.arg("show")
.arg(&interface_to_handle)
.arg("dump")
.output()?;
let output = if options.prepend_sudo {
Command::new("sudo")
.arg("wg")
.arg("show")
.arg(&interface_to_handle)
.arg("dump")
.output()?
} else {
Command::new("wg")
.arg("show")
.arg(&interface_to_handle)
.arg("dump")
.output()?
};
let output_stdout_str = String::from_utf8(output.stdout)?;
trace!(
"wg show {} dump stdout == {}",
@ -125,6 +135,12 @@ async fn main() {
.help("verbose logging")
.takes_value(false),
)
.arg(
Arg::with_name("prepend_sudo")
.short("a")
.help("Prepend sudo to the wg show commands")
.takes_value(false),
)
.arg(
Arg::with_name("separate_allowed_ips")
.short("s")

View file

@ -1,6 +1,7 @@
#[derive(Debug, Clone)]
pub(crate) struct Options {
pub verbose: bool,
pub prepend_sudo: bool,
pub separate_allowed_ips: bool,
pub extract_names_config_file: Option<String>,
pub interfaces: Option<Vec<String>>,
@ -11,6 +12,7 @@ impl Options {
pub fn from_claps(matches: &clap::ArgMatches<'_>) -> Options {
let options = Options {
verbose: matches.is_present("verbose"),
prepend_sudo: matches.is_present("prepend_sudo"),
separate_allowed_ips: matches.is_present("separate_allowed_ips"),
extract_names_config_file: matches
.value_of("extract_names_config_files")
@ -29,8 +31,3 @@ impl Options {
options
}
}
#[cfg(test)]
mod tests {
use super::*;
}