From 422024d9193a4218383e7b7e34f7c9d18c40461a Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Tue, 30 Mar 2021 22:51:51 +0200 Subject: [PATCH] main: support config override with CLI flag --- Cargo.lock | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 56 +++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 94 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c0de7d0..316ab18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,6 +65,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.9", +] + [[package]] name = "anyhow" version = "1.0.40" @@ -152,6 +161,21 @@ dependencies = [ "generic-array", ] +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "cookie" version = "0.11.4" @@ -509,6 +533,7 @@ name = "lohr" version = "0.3.0" dependencies = [ "anyhow", + "clap", "hex", "hmac", "log 0.4.14", @@ -955,6 +980,12 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3015a7d0a5fd5105c91c3710d42f9ccf0abfb287d62206484dcc67f9569a6483" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "subtle" version = "2.4.0" @@ -983,6 +1014,15 @@ dependencies = [ "unicode-xid 0.2.1", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "time" version = "0.1.43" @@ -1062,6 +1102,12 @@ dependencies = [ "tinyvec", ] +[[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" @@ -1095,6 +1141,12 @@ dependencies = [ "percent-encoding 1.0.1", ] +[[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" diff --git a/Cargo.toml b/Cargo.toml index cbbb9ae..f68cf99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ repository = "https://github.com/alarsyo/lohr" [dependencies] anyhow = "1.0.40" +clap = "2.33.3" hex = "0.4.3" hmac = "0.10.1" log = "0.4.14" diff --git a/src/main.rs b/src/main.rs index 812615d..bedcdf7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,16 +2,17 @@ use std::env; use std::fs::File; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::sync::{ mpsc::{channel, Receiver, Sender}, Mutex, }; use std::thread; -use rocket::{http::Status, post, routes, State}; - +use anyhow::Context; +use clap::{App, Arg}; use log::error; +use rocket::{http::Status, post, routes, State}; mod gitea; use gitea::GiteaWebHook; @@ -61,21 +62,46 @@ fn repo_updater(rx: Receiver, homedir: PathBuf, config: GlobalSettings) { } } -fn parse_config(mut path: PathBuf) -> anyhow::Result { - path.push("lohr-config"); - path.set_extension("yaml"); - let path = env::var("LOHR_CONFIG") - .map(Into::into) - .unwrap_or_else(|_| path); - let config = if let Ok(file) = File::open(path.as_path()) { - serde_yaml::from_reader(file)? - } else { - Default::default() +fn parse_config(home: &Path, flags: &clap::ArgMatches) -> anyhow::Result { + // prioritize CLI flag, then env var + let config_path = flags.value_of("config").map(PathBuf::from); + let config_path = config_path.or_else(|| env::var("LOHR_CONFIG").map(PathBuf::from).ok()); + + let file = match config_path { + Some(config_path) => File::open(&config_path).with_context(|| { + format!( + "could not open provided configuration file at {}", + config_path.display() + ) + })?, + None => { + // check if file exists in lohr home + let config_path = home.join("lohr-config.yaml"); + if !config_path.is_file() { + return Ok(Default::default()); + } + + File::open(config_path).context("failed to open configuration file in LOHR_HOME")? + } }; - Ok(config) + + serde_yaml::from_reader(file).context("could not parse configuration file") } fn main() -> anyhow::Result<()> { + let matches = App::new("lohr") + .version("0.3.0") + .about("Git mirroring daemon") + .arg( + Arg::with_name("config") + .short("c") + .long("config") + .value_name("FILE") + .help("Use a custom config file") + .takes_value(true), + ) + .get_matches(); + let (sender, receiver) = channel(); let homedir = env::var("LOHR_HOME").unwrap_or_else(|_| "./".to_string()); @@ -85,7 +111,7 @@ fn main() -> anyhow::Result<()> { let secret = env::var("LOHR_SECRET") .expect("please provide a secret, otherwise anyone can send you a malicious webhook"); - let config = parse_config(homedir.clone())?; + let config = parse_config(&homedir, &matches)?; let config_state = config.clone(); thread::spawn(move || {