From 1595b36181982d24eb26f9b04f7c183688367433 Mon Sep 17 00:00:00 2001 From: Mingwei Zhang Date: Tue, 28 Nov 2023 09:36:57 -0800 Subject: [PATCH] allow loading env file in cli --- Cargo.toml | 3 ++- src/cli/main.rs | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 179351e..94290d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ indicatif = { version = "0.17.7", optional = true } futures-util = { version = "0.3.28", optional = true } itertools = { version = "0.12.0" , optional = true } tempfile = "3.8" +dotenvy = { version = "0.15" , optional = true } # crawler dependencies futures = {version="0.3", optional = true} @@ -65,7 +66,7 @@ sqlx = { version = "0.7", features = [ "runtime-tokio", "sqlite"], optional = tr default=[] cli = [ # command-line interface - "clap", "dirs", "humantime", "num_cpus", "tracing-subscriber", "tabled", "itertools", + "clap", "dirs", "humantime", "num_cpus", "tracing-subscriber", "tabled", "itertools", "dotenvy", # crawler "futures", "oneio", "regex", "scraper", "tokio", "lazy_static", # database diff --git a/src/cli/main.rs b/src/cli/main.rs index dadf893..3968efe 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -50,6 +50,9 @@ struct Cli { #[clap(long, global = true)] no_log: bool, + #[clap(long, global = true)] + env: Option, + #[clap(subcommand)] command: Commands, } @@ -255,6 +258,18 @@ fn main() { let do_log = !cli.no_log; + if let Some(env_path) = cli.env { + match dotenvy::from_path_override(env_path.as_str()) { + Ok(_) => { + info!("loaded environment variables from {}", env_path); + } + Err(_) => { + error!("failed to load environment variables from {}", env_path); + exit(1); + } + }; + } + if std::env::var_os("RUST_LOG").is_none() { std::env::set_var("RUST_LOG", "bgpkit_broker=info,poem=debug"); }