From 80d35cb60eee1072ca24978c3114bf4cca3cd7f4 Mon Sep 17 00:00:00 2001 From: Lev Kokotov Date: Thu, 20 Jul 2023 23:17:28 -0700 Subject: [PATCH] Load all .env.* files --- dotenv/src/lib.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/dotenv/src/lib.rs b/dotenv/src/lib.rs index f2b1b0a..51bb454 100644 --- a/dotenv/src/lib.rs +++ b/dotenv/src/lib.rs @@ -151,17 +151,38 @@ pub fn from_filename_iter>(filename: P) -> Result> { } /// This is usually what you want. -/// It loads the .env file located in the environment's current directory or its parents in sequence. +/// +/// It loads the .env files located in the environment's current directory or its parents in sequence. +/// Multiple files are attempted in order of priority. Variables in lower priority files get overwritten +/// by variables in higher priority files. +/// +/// Order of files is the same as the Ruby dotenv gem, see . /// /// # Examples /// ``` /// use dotenv; /// dotenv::dotenv().ok(); /// ``` -pub fn dotenv() -> Result { - let (path, iter) = Finder::new().find()?; - iter.load()?; - Ok(path) +pub fn dotenv() -> Result<()> { + for file in [ + ".env", + ".env.production", + ".env.test", + ".env.development", + ".env.local", + ".env.production.local", + ".env.test.local", + ".env.development.local", + ] { + let finder = Finder::new().filename(&Path::new(file)); + + match finder.find() { + Ok((_path, iter)) => iter.load()?, + Err(_) => continue, + }; + } + + Ok(()) } /// Like `dotenv`, but returns an iterator over variables instead of loading into environment.