From 3f1c091b871a2950c96fb767e18fc6fbb31486c3 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 31 Jul 2024 12:41:09 -0700 Subject: [PATCH] Add the ability to store secret dev-only env vars in .env.secret.toml (#15557) Release Notes: - N/A --------- Co-authored-by: Marshall --- .gitignore | 3 +++ crates/collab/src/bin/dotenv.rs | 16 ++-------------- crates/collab/src/env.rs | 32 ++++++++++++++++++++++++-------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index de15d0abc3..634b73ac94 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,6 @@ DerivedData/ .vscode .wrangler .flatpak-builder + +# Don't commit any secrets to the repo. +.env.secret.toml diff --git a/crates/collab/src/bin/dotenv.rs b/crates/collab/src/bin/dotenv.rs index c093bcb6e9..b2f5af9b39 100644 --- a/crates/collab/src/bin/dotenv.rs +++ b/crates/collab/src/bin/dotenv.rs @@ -1,20 +1,8 @@ -use anyhow::anyhow; -use std::fs; +use collab::env::get_dotenv_vars; fn main() -> anyhow::Result<()> { - let env: toml::map::Map = toml::de::from_str( - &fs::read_to_string("./.env.toml").map_err(|_| anyhow!("no .env.toml file found"))?, - )?; - - for (key, value) in env { - let value = match value { - toml::Value::String(value) => value, - toml::Value::Integer(value) => value.to_string(), - toml::Value::Float(value) => value.to_string(), - _ => panic!("unsupported TOML value in .env.toml for key {}", key), - }; + for (key, value) in get_dotenv_vars()? { println!("export {}=\"{}\"", key, value); } - Ok(()) } diff --git a/crates/collab/src/env.rs b/crates/collab/src/env.rs index 4e6fe3b3a3..f5e316bb4a 100644 --- a/crates/collab/src/env.rs +++ b/crates/collab/src/env.rs @@ -1,12 +1,29 @@ -use anyhow::anyhow; +use anyhow::{anyhow, Result}; use std::fs; -pub fn load_dotenv() -> anyhow::Result<()> { - let env: toml::map::Map = toml::de::from_str( - &fs::read_to_string("./crates/collab/.env.toml") - .map_err(|_| anyhow!("no .env.toml file found"))?, - )?; +pub fn get_dotenv_vars() -> Result> { + let mut vars = Vec::new(); + let env_content = fs::read_to_string("./crates/collab/.env.toml") + .map_err(|_| anyhow!("no .env.toml file found"))?; + add_vars(env_content, &mut vars)?; + + if let Ok(secret_content) = fs::read_to_string("./crates/collab/.env.secret.toml") { + add_vars(secret_content, &mut vars)?; + } + + Ok(vars) +} + +pub fn load_dotenv() -> Result<()> { + for (key, value) in get_dotenv_vars()? { + std::env::set_var(key, value); + } + Ok(()) +} + +fn add_vars(env_content: String, vars: &mut Vec<(String, String)>) -> Result<()> { + let env: toml::map::Map = toml::de::from_str(&env_content)?; for (key, value) in env { let value = match value { toml::Value::String(value) => value, @@ -14,8 +31,7 @@ pub fn load_dotenv() -> anyhow::Result<()> { toml::Value::Float(value) => value.to_string(), _ => panic!("unsupported TOML value in .env.toml for key {}", key), }; - std::env::set_var(key, value); + vars.push((key, value)); } - Ok(()) }