main: support config override with CLI flag

This commit is contained in:
Antoine Martin 2021-03-30 22:51:51 +02:00
parent ff90b5fb2d
commit 422024d919
3 changed files with 94 additions and 15 deletions

52
Cargo.lock generated
View file

@ -65,6 +65,15 @@ dependencies = [
"memchr", "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]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.40" version = "1.0.40"
@ -152,6 +161,21 @@ dependencies = [
"generic-array", "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]] [[package]]
name = "cookie" name = "cookie"
version = "0.11.4" version = "0.11.4"
@ -509,6 +533,7 @@ name = "lohr"
version = "0.3.0" version = "0.3.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap",
"hex", "hex",
"hmac", "hmac",
"log 0.4.14", "log 0.4.14",
@ -955,6 +980,12 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3015a7d0a5fd5105c91c3710d42f9ccf0abfb287d62206484dcc67f9569a6483" checksum = "3015a7d0a5fd5105c91c3710d42f9ccf0abfb287d62206484dcc67f9569a6483"
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]] [[package]]
name = "subtle" name = "subtle"
version = "2.4.0" version = "2.4.0"
@ -983,6 +1014,15 @@ dependencies = [
"unicode-xid 0.2.1", "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]] [[package]]
name = "time" name = "time"
version = "0.1.43" version = "0.1.43"
@ -1062,6 +1102,12 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.1.0" version = "0.1.0"
@ -1095,6 +1141,12 @@ dependencies = [
"percent-encoding 1.0.1", "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]] [[package]]
name = "version_check" name = "version_check"
version = "0.1.5" version = "0.1.5"

View file

@ -12,6 +12,7 @@ repository = "https://github.com/alarsyo/lohr"
[dependencies] [dependencies]
anyhow = "1.0.40" anyhow = "1.0.40"
clap = "2.33.3"
hex = "0.4.3" hex = "0.4.3"
hmac = "0.10.1" hmac = "0.10.1"
log = "0.4.14" log = "0.4.14"

View file

@ -2,16 +2,17 @@
use std::env; use std::env;
use std::fs::File; use std::fs::File;
use std::path::PathBuf; use std::path::{Path, PathBuf};
use std::sync::{ use std::sync::{
mpsc::{channel, Receiver, Sender}, mpsc::{channel, Receiver, Sender},
Mutex, Mutex,
}; };
use std::thread; use std::thread;
use rocket::{http::Status, post, routes, State}; use anyhow::Context;
use clap::{App, Arg};
use log::error; use log::error;
use rocket::{http::Status, post, routes, State};
mod gitea; mod gitea;
use gitea::GiteaWebHook; use gitea::GiteaWebHook;
@ -61,21 +62,46 @@ fn repo_updater(rx: Receiver<Job>, homedir: PathBuf, config: GlobalSettings) {
} }
} }
fn parse_config(mut path: PathBuf) -> anyhow::Result<GlobalSettings> { fn parse_config(home: &Path, flags: &clap::ArgMatches) -> anyhow::Result<GlobalSettings> {
path.push("lohr-config"); // prioritize CLI flag, then env var
path.set_extension("yaml"); let config_path = flags.value_of("config").map(PathBuf::from);
let path = env::var("LOHR_CONFIG") let config_path = config_path.or_else(|| env::var("LOHR_CONFIG").map(PathBuf::from).ok());
.map(Into::into)
.unwrap_or_else(|_| path); let file = match config_path {
let config = if let Ok(file) = File::open(path.as_path()) { Some(config_path) => File::open(&config_path).with_context(|| {
serde_yaml::from_reader(file)? format!(
} else { "could not open provided configuration file at {}",
Default::default() 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<()> { 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 (sender, receiver) = channel();
let homedir = env::var("LOHR_HOME").unwrap_or_else(|_| "./".to_string()); 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") let secret = env::var("LOHR_SECRET")
.expect("please provide a secret, otherwise anyone can send you a malicious webhook"); .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(); let config_state = config.clone();
thread::spawn(move || { thread::spawn(move || {