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.
This commit is contained in:
finga 2021-05-31 16:14:19 +02:00
parent 43b7fd5625
commit d29bfdf88d
3 changed files with 132 additions and 1 deletions

View file

@ -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<File> {
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| {