Add -V, --version argument, env variables only apply for long options
This commit is contained in:
parent
2c902bdddf
commit
d026f700a5
@ -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 <seconds> 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.
|
||||
|
@ -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.
|
||||
|
@ -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 <seconds> 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.
|
||||
|
26
src/lib.rs
26
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<String> {
|
||||
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<String> {
|
||||
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<String> {
|
||||
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user