From dd1b72f24fdd1047973d5bd575cb9ae10424c7e4 Mon Sep 17 00:00:00 2001 From: indirection42 Date: Mon, 22 Apr 2024 14:22:41 +0800 Subject: [PATCH] change env replacement pattern --- README.md | 2 +- src/config/mod.rs | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c9ec2ff..aefb7cf 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Run with `RUSTFLAGS="--cfg tokio_unstable"` to enable [tokio-console](https://gi - Log format. Default: `full`. - Options: `full`, `pretty`, `json`, `compact` -In addition, you can refer env variables in `config.yml` by using `${env.SOME_ENV}` +In addition, you can refer env variables in `config.yml` by using `${SOME_ENV}` ## Features diff --git a/src/config/mod.rs b/src/config/mod.rs index 394a897..41fd6fa 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -170,8 +170,8 @@ pub fn read_config() -> Result { } fn render_template(templated_config_str: &str) -> Result { - // match pattern: ${env.SOME_VAR} - let re = Regex::new(r"\$\{env\.([^\}]+)\}").unwrap(); + // match pattern: ${SOME_VAR} + let re = Regex::new(r"\$\{([^\}]+)\}").unwrap(); let mut config_str = String::with_capacity(templated_config_str.len()); let mut last_match = 0; @@ -228,15 +228,16 @@ mod tests { use super::*; #[test] - fn test_render_template() { + fn render_template_basically_works() { env::set_var("KEY", "value"); env::set_var("ANOTHER_KEY", "another_value"); - let templated_config_str = "${env.KEY} ${env.ANOTHER_KEY}"; + let templated_config_str = "${KEY} ${ANOTHER_KEY}"; let config_str = render_template(templated_config_str).unwrap(); assert_eq!(config_str, "value another_value"); env::remove_var("KEY"); let config_str = render_template(templated_config_str); assert!(config_str.is_err()); + env::remove_var("ANOTHER_KEY"); } }