Skip to content

Commit

Permalink
change env replacement pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
indirection42 committed Apr 22, 2024
1 parent 87552f7 commit dd1b72f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ pub fn read_config() -> Result<Config, anyhow::Error> {
}

fn render_template(templated_config_str: &str) -> Result<String, anyhow::Error> {
// 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;
Expand Down Expand Up @@ -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");
}
}

0 comments on commit dd1b72f

Please sign in to comment.