From cc404b4edc392fc141c941da10e411d8e98a817e Mon Sep 17 00:00:00 2001 From: zethra Date: Mon, 2 Mar 2020 14:57:06 -0500 Subject: [PATCH] Added command line flags for help and version Signed-off-by: zethra --- Cargo.lock | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 5 +- src/main.rs | 27 ++++++++++- 3 files changed, 158 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d978da9..232f724 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,6 +15,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi 0.3.8", +] + [[package]] name = "arrayvec" version = "0.4.12" @@ -144,6 +153,7 @@ dependencies = [ "serde_derive", "serde_json", "soup", + "structopt", "syslog", "u2f", "uuid 0.8.1", @@ -259,6 +269,21 @@ dependencies = [ "time 0.1.42", ] +[[package]] +name = "clap" +version = "2.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "cloudabi" version = "0.0.3" @@ -868,6 +893,15 @@ dependencies = [ "walkdir", ] +[[package]] +name = "heck" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "hermit-abi" version = "0.1.6" @@ -1797,6 +1831,32 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" +[[package]] +name = "proc-macro-error" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7959c6467d962050d639361f7703b2051c43036d03493c36f01d440fdd3138a" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2 1.0.8", + "quote 1.0.2", + "syn 1.0.14", + "version_check 0.9.1", +] + +[[package]] +name = "proc-macro-error-attr" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4002d9f55991d5e019fb940a90e1a95eb80c24e77cb2462dd4dc869604d543a" +dependencies = [ + "proc-macro2 1.0.8", + "quote 1.0.2", + "syn 1.0.14", + "syn-mid", + "version_check 0.9.1", +] + [[package]] name = "proc-macro-hack" version = "0.5.11" @@ -2554,6 +2614,36 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1884d1bc09741d466d9b14e6d37ac89d6909cbcac41dd9ae982d4d063bbedfc" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "structopt" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fe43617218c0805c6eb37160119dc3c548110a67786da7218d1c6555212f073" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6e79c80e0f4efd86ca960218d4e056249be189ff1c42824dcd9a7f51a56f0bd" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2 1.0.8", + "quote 1.0.2", + "syn 1.0.14", +] + [[package]] name = "subtle" version = "1.0.0" @@ -2599,6 +2689,17 @@ dependencies = [ "unicode-xid 0.2.0", ] +[[package]] +name = "syn-mid" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a" +dependencies = [ + "proc-macro2 1.0.8", + "quote 1.0.2", + "syn 1.0.14", +] + [[package]] name = "synstructure" version = "0.12.3" @@ -2648,6 +2749,15 @@ dependencies = [ "utf-8", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "thread_local" version = "1.0.1" @@ -2952,6 +3062,18 @@ dependencies = [ "smallvec 1.2.0", ] +[[package]] +name = "unicode-segmentation" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" + +[[package]] +name = "unicode-width" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" + [[package]] name = "unicode-xid" version = "0.1.0" @@ -3022,6 +3144,12 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fc439f2794e98976c88a2a2dafce96b930fe8010b0a256b3c2199a773933168" +[[package]] +name = "vec_map" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" + [[package]] name = "version_check" version = "0.1.5" diff --git a/Cargo.toml b/Cargo.toml index 330250e..7cfe970 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,7 @@ fern = { version = "0.5.9", features = ["syslog-4"] } diesel = { version = "1.4.3", features = [ "chrono", "r2d2"] } diesel_migrations = "1.4.0" -# Bundled SQLite +# Bundled SQLite libsqlite3-sys = { version = "0.16.0", features = ["bundled"], optional = true } # Crypto library @@ -114,6 +114,9 @@ percent-encoding = "2.1.0" # Punycode conversion idna = "0.2.0" +# CLI argument parsing +structopt = "0.3.11" + [patch.crates-io] # Use newest ring rocket = { git = 'https://github.com/SergioBenitez/Rocket', rev = 'b95b6765e1cc8be7c1e7eaef8a9d9ad940b0ac13' } diff --git a/src/main.rs b/src/main.rs index 3c12063..5274795 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,18 @@ mod util; pub use config::CONFIG; pub use error::{Error, MapResult}; +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +#[structopt(name = "bitwarden_rs", about = "A Bitwarden API server written in Rust")] +struct Opt { + /// Prints the app version + #[structopt(short, long)] + version: bool, +} + fn main() { + parse_args(); launch_info(); use log::LevelFilter as LF; @@ -64,6 +75,18 @@ fn main() { launch_rocket(extra_debug); } +fn parse_args() { + let opt = Opt::from_args(); + if opt.version { + if let Some(version) = option_env!("GIT_VERSION") { + println!("bitwarden_rs {}", version); + } else { + println!("bitwarden_rs (Version info from Git not present)"); + } + exit(0); + } +} + fn launch_info() { println!("/--------------------------------------------------------------------\\"); println!("| Starting Bitwarden_RS |"); @@ -177,7 +200,9 @@ fn check_rsa_keys() { info!("JWT keys don't exist, checking if OpenSSL is available..."); Command::new("openssl").arg("version").status().unwrap_or_else(|_| { - info!("Can't create keys because OpenSSL is not available, make sure it's installed and available on the PATH"); + info!( + "Can't create keys because OpenSSL is not available, make sure it's installed and available on the PATH" + ); exit(1); });