From d29bfdf88d6b5d02b41fbee5e120db7f20d34eee Mon Sep 17 00:00:00 2001 From: finga Date: Mon, 31 May 2021 16:14:19 +0200 Subject: [PATCH] Use clap for command line arguments To support command line arguments `clap` is used. This adds following argument `--config`/`-c` to provide a different path for the configurationf file and the subcommand `configtest` which parses and loads the configuration and exits. --- Cargo.lock | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 27 +++++++++++++- 3 files changed, 132 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index b21d842..1f0f72c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -164,6 +164,38 @@ dependencies = [ "generic-array", ] +[[package]] +name = "clap" +version = "3.0.0-beta.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "indexmap", + "lazy_static", + "os_str_bytes", + "strsim", + "termcolor", + "textwrap", + "unicode-width", + "vec_map", +] + +[[package]] +name = "clap_derive" +version = "3.0.0-beta.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "370f715b81112975b1b69db93e0b56ea4cd4e5002ac43b2da8474106a54096a1" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2 1.0.27", + "quote 1.0.9", + "syn 1.0.72", +] + [[package]] name = "cookie" version = "0.11.4" @@ -353,6 +385,15 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" +[[package]] +name = "heck" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "hermit-abi" version = "0.1.18" @@ -572,6 +613,12 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "os_str_bytes" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85" + [[package]] name = "pear" version = "0.1.4" @@ -623,6 +670,30 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2 1.0.27", + "quote 1.0.9", + "syn 1.0.72", + "version_check 0.9.3", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2 1.0.27", + "quote 1.0.9", + "version_check 0.9.3", +] + [[package]] name = "proc-macro2" version = "0.4.30" @@ -927,6 +998,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "subtle" version = "2.4.0" @@ -970,6 +1047,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "textwrap" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789" +dependencies = [ + "unicode-width", +] + [[package]] name = "thiserror" version = "1.0.25" @@ -1069,6 +1155,18 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" + +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + [[package]] name = "unicode-xid" version = "0.1.0" @@ -1118,6 +1216,12 @@ dependencies = [ "log 0.4.14", ] +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.1.5" @@ -1141,6 +1245,7 @@ name = "webhookey" version = "0.1.0-rc.2" dependencies = [ "anyhow", + "clap", "dirs", "env_logger", "hex", diff --git a/Cargo.toml b/Cargo.toml index 0a021c0..6506c0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ hex = "0.4" ipnet = { version = "2.3", features = ["serde"] } thiserror = "1.0" run_script = "0.7" +clap = "3.0.0-beta.2" [package.metadata.deb] extended-description = "Webhookey receives requests in form of a so called Webhook as for example sent by Gitea. Those requests are matched against configured filters, if a filter matches, values from the header and the body can be passed to scripts as parameters which are then executed subsequently." diff --git a/src/main.rs b/src/main.rs index 78704b9..3494d91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![feature(proc_macro_hygiene, decl_macro)] use anyhow::{anyhow, bail, Result}; +use clap::{app_from_crate, App, Arg}; use hmac::{Hmac, Mac, NewMac}; use ipnet::IpNet; use log::{debug, error, info, trace, warn}; @@ -474,10 +475,34 @@ fn get_config() -> Result { fn main() -> Result<()> { env_logger::init(); - let config: Config = serde_yaml::from_reader(BufReader::new(get_config()?))?; + let cli = app_from_crate!() + .arg( + Arg::new("config") + .short('c') + .long("config") + .takes_value(true) + .value_name("FILE") + .about("Provide a path to the configuration file"), + ) + .subcommand( + App::new("configtest") + .about("Verifies if the configuration can be parsed without errors."), + ) + .get_matches(); + + let config: Config = match cli.value_of("config") { + Some(config) => serde_yaml::from_reader(BufReader::new(File::open(config)?))?, + _ => serde_yaml::from_reader(BufReader::new(get_config()?))?, + }; trace!("Parsed configuration:\n{}", serde_yaml::to_string(&config)?); + if let Some(_) = cli.subcommand_matches("configtest") { + debug!("Configtest succeded."); + println!("Config is OK"); + return Ok(()) + } + rocket::ignite() .mount("/", routes![receive_hook]) .attach(AdHoc::on_attach("webhookey config", move |rocket| {