From d026f700a55271dd0d97a569e1c7e1dd9db4e26e Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Sun, 22 Dec 2019 23:59:42 -0500 Subject: [PATCH] Add -V, --version argument, env variables only apply for long options --- README.md | 5 +++-- src/bin/udp-test.rs | 17 +++++++++++++++-- src/bin/wireguard-proxy.rs | 16 ++++++++++++++-- src/lib.rs | 26 +++++++++++++++++--------- 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 235fd2a..7837e2e 100644 --- a/README.md +++ b/README.md @@ -51,12 +51,13 @@ usage: wireguard-proxy [options...] Common Options: -h, --help print this usage text + -V, --version Show version number and TLS support then quit -st, --socket-timeout Socket timeout (time to wait for data) before terminating, default: 0 Environment variable support: - For every command line option, short and long, if you replace all - leading - with WGP_, and replace all remaining - with _, and uppercase + For every long command line option (starting with --), if you replace the + leading -- with WGP_, and replace all remaining - with _, and uppercase the whole thing, if you don't specify that command line option we will read that environment variable for the argument. boolean arguments are true if anything but unset, empty, 0, or false. diff --git a/src/bin/udp-test.rs b/src/bin/udp-test.rs index 614c9c0..1bb3231 100644 --- a/src/bin/udp-test.rs +++ b/src/bin/udp-test.rs @@ -86,6 +86,18 @@ impl Server { fn main() { let raw_args = env::args().collect(); let args = Args::new(&raw_args); + + if args.flag("-V") || args.flag("--version") { + print!("udp-test {} ", env!("CARGO_PKG_VERSION")); + #[cfg(not(any(feature = "tls", feature = "openssl_vendored")))] + println!("TLS support: None"); + #[cfg(feature = "openssl_vendored")] + println!("TLS support: Static/Vendored OpenSSL"); + #[cfg(feature = "tls")] + println!("TLS support: System OpenSSL"); + return; + } + let default_udp_host_target = "127.0.0.1:51820"; let default_socket_timeout = 10; @@ -106,6 +118,7 @@ fn main() { if args.flag("-h") || args.flag("--help") { println!(r#"usage: udp-test [options...] -h, --help print this usage text + -V, --version Show version number and TLS support then quit -s, --self-test run a self test through proxy -is, --internal-self-test run a self test through proxy without spawning other processes @@ -130,8 +143,8 @@ fn main() { one pem file Environment variable support: - For every command line option, short and long, if you replace all - leading - with WGP_, and replace all remaining - with _, and uppercase + For every long command line option (starting with --), if you replace the + leading -- with WGP_, and replace all remaining - with _, and uppercase the whole thing, if you don't specify that command line option we will read that environment variable for the argument. boolean arguments are true if anything but unset, empty, 0, or false. diff --git a/src/bin/wireguard-proxy.rs b/src/bin/wireguard-proxy.rs index 1a25431..ef3e7b9 100644 --- a/src/bin/wireguard-proxy.rs +++ b/src/bin/wireguard-proxy.rs @@ -5,6 +5,17 @@ fn main() { let raw_args = env::args().collect(); let args = Args::new(&raw_args); + if args.flag("-V") || args.flag("--version") { + print!("wireguard-proxy {} ", env!("CARGO_PKG_VERSION")); + #[cfg(not(any(feature = "tls", feature = "openssl_vendored")))] + println!("TLS support: None"); + #[cfg(feature = "openssl_vendored")] + println!("TLS support: Static/Vendored OpenSSL"); + #[cfg(feature = "tls")] + println!("TLS support: System OpenSSL"); + return; + } + let default_udp_host_target = "127.0.0.1:51820"; let default_socket_timeout = 0; @@ -56,12 +67,13 @@ fn main() { Common Options: -h, --help print this usage text + -V, --version Show version number and TLS support then quit -st, --socket-timeout Socket timeout (time to wait for data) before terminating, default: {} Environment variable support: - For every command line option, short and long, if you replace all - leading - with WGP_, and replace all remaining - with _, and uppercase + For every long command line option (starting with --), if you replace the + leading -- with WGP_, and replace all remaining - with _, and uppercase the whole thing, if you don't specify that command line option we will read that environment variable for the argument. boolean arguments are true if anything but unset, empty, 0, or false. diff --git a/src/lib.rs b/src/lib.rs index 98b1936..3adb5e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,11 +24,18 @@ mod tls { use tls::{TlsStream, TlsListener}; -fn arg_to_env<'a>(arg: &'a str) -> String { +fn arg_to_env(arg: &str) -> Option { + if !arg.starts_with("--") { + return None; + } let env = "WGP_".to_owned(); let mut env = env + &arg.trim_matches('-').replace("-", "_"); env.make_ascii_uppercase(); - env + Some(env) +} + +fn env_for_arg(arg: &str) -> Option { + arg_to_env(arg).and_then(|key| std::env::var(key).ok()) } #[cfg(test)] @@ -37,9 +44,10 @@ mod tests { #[test] fn test_arg_to_env() { - assert_eq!(arg_to_env("--tcp-host"), "WGP_TCP_HOST"); - assert_eq!(arg_to_env("--tls"), "WGP_TLS"); - assert_eq!(arg_to_env("-h"), "WGP_H"); + assert_eq!(arg_to_env("--tcp-host"), Some("WGP_TCP_HOST".to_owned())); + assert_eq!(arg_to_env("--tls"), Some("WGP_TLS".to_owned())); + assert_eq!(arg_to_env("-h"), None); + assert_eq!(arg_to_env("-th"), None); } } @@ -56,9 +64,9 @@ impl<'a> Args<'a> { return true; } // because env we want slightly special handling of empty/0/false - match std::env::var(arg_to_env(flag)) { - Ok(env) => &env != "" && &env != "0" && &env != "false", - Err(_) => false, + match env_for_arg(flag) { + Some(env) => &env != "" && &env != "0" && &env != "false", + None => false, } } pub fn get_option(&self, flags: &[&'a str]) -> Option { @@ -75,7 +83,7 @@ impl<'a> Args<'a> { } // no matching arguments are found, so check env variables as a fallback for flag in flags.iter() { - let env = std::env::var(arg_to_env(flag)).ok(); + let env = env_for_arg(flag); if env.is_some() { return env; }