Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ablity to use own enviornment variables in config files #344

Merged
merged 14 commits into from
Oct 8, 2024
Merged

Ablity to use own enviornment variables in config files #344

merged 14 commits into from
Oct 8, 2024

Conversation

taslimmuhammed
Copy link
Contributor

Fixes #336
I have tested the function alone in a separate environment, please do test it with entire project.

@gusinacio
Copy link
Contributor

Hello, just a few things before I review this PR.

  1. Could you do a git rebase from our main branch?
  2. Could you add some unit tests checking that this regex work?

Also, is there any reason to come up with your own regex over using a crate like envsubst?

Please don't change your solution, I'm just curious to know your thoughts.

@taslimmuhammed
Copy link
Contributor Author

Actually no, I didn't know about envsubst, could you tell me how to add unit tests?, I had tested using a toml file, it works well with it.

@gusinacio
Copy link
Contributor

gusinacio commented Oct 4, 2024

Down the config.rs file, there's a mod test. You can add a new #[test] function there and test it. You can use the other as examples.

https://github.com/taslimmuhammed/indexer-rs/blob/8844093474289564b2bd7770a3dbd1beb94170c3/config/src/config.rs#L303-L330

You can create a unit test for substitute_env_vars() function, where you pass a predefined String, set some environment variables and check if the output is what you expected.

@taslimmuhammed
Copy link
Contributor Author

thanks✌️, I'll do that

@taslimmuhammed
Copy link
Contributor Author

I have added test for only substitute_env_vars() alone not combining test_override_with_env, if required I'll add.

Copy link
Contributor

@gusinacio gusinacio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, just a small adjustment and this is ready to be merged.

@taslimmuhammed
Copy link
Contributor Author

made more changes:-

  • if the env variable is not found then it'll send error
  • added a new test combining parsing and substitution of custom env vars, working good
    please have a look, and BTW if you think the code is getting undauntedly lengthy lets use the package it's fine

@taslimmuhammed
Copy link
Contributor Author

also please note:- in some places i have replaced std::env to just env

Copy link
Contributor

@gusinacio gusinacio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +113 to +118
if !missing_vars.is_empty() {
return Err(format!(
"Missing environment variables: {}",
missing_vars.join(", ")
));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you have this to inform users about env vars missing.

let input = r#"
[section1]
key1 = "${TEST_VAR1}"
key2 = "${TEST_VAR-default}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a future PR: we should ignore default variables or let the user know that we don't support it.

let mut result = String::new();

for line in content.lines() {
if !line.trim_start().starts_with('#') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that you are ignoring comment lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fortunately there was a variable in the comments of minimal_config, which failed the new test[✌️]

@gusinacio gusinacio merged commit 1db3adb into graphprotocol:main Oct 8, 2024
7 of 10 checks passed
@taslimmuhammed
Copy link
Contributor Author

Thanks alot 🙌

@github-actions github-actions bot mentioned this pull request Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add envsubst for config files
2 participants