generated from PaulRBerg/hardhat-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Env substitution works * Move to dev-dependencies * Formatting * Ensure that there are no quotes or anything around substitution * use non normal env vars to avoid future issues
- Loading branch information
Showing
6 changed files
with
155 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
mod app_config; | ||
mod yaml; | ||
pub use app_config::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use anyhow::Result; | ||
use std::{fs, path::PathBuf}; | ||
|
||
pub fn load_yaml_with_env(file_path: &PathBuf) -> Result<String> { | ||
// Read the file content to string | ||
let content = match fs::read_to_string(file_path) { | ||
Ok(val) => val, | ||
Err(_) => "".to_string(), | ||
}; | ||
|
||
// Collect environment variables and perform substitution | ||
Ok(shellexpand::env(&content)?.to_string()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::env; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use tempfile::tempdir; | ||
|
||
#[test] | ||
fn test_yaml_env_substitution() -> Result<()> { | ||
// Create a temporary directory and file | ||
let dir = tempdir()?; | ||
let file_path = dir.path().join("test.yaml"); | ||
let mut file = File::create(&file_path)?; | ||
|
||
// Write test YAML content | ||
writeln!( | ||
file, | ||
"database:\n url: $MY_DATABASE_URL\n password: ${{MY_DB_PASSWORD}}" | ||
)?; | ||
|
||
// Set environment variables | ||
env::set_var("MY_DATABASE_URL", "postgres://localhost:5432"); | ||
env::set_var("MY_DB_PASSWORD", "secret123"); | ||
|
||
// Test the function | ||
let processed = load_yaml_with_env(&file_path)?; | ||
|
||
env::remove_var("MY_DATABASE_URL"); | ||
env::remove_var("MY_DB_PASSWORD"); | ||
|
||
assert!(processed.contains("postgres://localhost:5432")); | ||
assert!(processed.contains("secret123")); | ||
|
||
Ok(()) | ||
} | ||
} |