From 95bde28ebe395f6c034c27f883122e0a0726ffdb Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Tue, 14 Dec 2021 16:27:18 +0100 Subject: [PATCH] Soft-fail if .env fails to load --- crates/cli/src/main.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index be3bde27..a2a97bca 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -105,11 +105,10 @@ async fn main() -> anyhow::Result<()> { async fn try_main() -> anyhow::Result<()> { // Load environment variables from .env files // We keep the path to log it afterwards - let dotenv_path: Option = dotenv::dotenv() + let dotenv_path: Result, _> = dotenv::dotenv() .map(Some) // Display the error if it is something other than the .env file not existing - .or_else(|e| if e.not_found() { Ok(None) } else { Err(e) }) - .context("failed to load .env file")?; + .or_else(|e| if e.not_found() { Ok(None) } else { Err(e) }); // Setup logging // This writes logs to stderr @@ -134,8 +133,10 @@ async fn try_main() -> anyhow::Result<()> { // Now that logging is set up, we can log stuff, like if the .env file was // loaded or not - if let Some(path) = dotenv_path { - tracing::info!(?path, "Loaded environment variables from file"); + match dotenv_path { + Ok(Some(path)) => tracing::info!(?path, "Loaded environment variables from file"), + Ok(None) => {} + Err(err) => tracing::warn!(%err, "failed to load .env file"), } // Parse the CLI arguments