config-rs icon indicating copy to clipboard operation
config-rs copied to clipboard

reading values via environment variables in bash

Open 8de2fdb0 opened this issue 1 year ago • 4 comments

Reading nested environment variables is kind of impossible when using this library.

Env variables like: APP.NESTED_CONFIG.VALUE

Don't really work in Bash, the workaround is to use a double underscore for separators __, I guess parsing would have been a bit harder if _ would be supported in that case. But from a usability perspective this is a real foodgun.

8de2fdb0 avatar Jun 25 '24 14:06 8de2fdb0

In fact, dots in environment variables aren't POSIX compliant. See https://stackoverflow.com/questions/2821043/allowed-characters-in-linux-environment-variable-names/2821201#2821201

softdevca avatar Jul 20 '24 05:07 softdevca

Also having problems with this. Ideally a pattern like ENVIRONMENT_N_CELLS=4 should be able to match a variable environment.n_cells. A possible solution is:

1. split the variable at the first occurrence of the separator
2. check for matches
3. if none is found, split at the second occurrence
4. repeat to exhaustion

This would have the side effect that a.b_c and a.b.c would be conflicting names for the struct variables, however.

aleferna12 avatar Jun 29 '25 14:06 aleferna12

Environment::separator should be used to separate nested keys, e.g. https://github.com/rust-cli/config-rs/blob/7213a4cedd4dc24828c6c2c973b07b14b93108ae/examples/env-list/main.rs#L10-L18

epage avatar Jun 30 '25 15:06 epage

Also having problems with this. Ideally a pattern like ENVIRONMENT_N_CELLS=4 should be able to match a variable environment.n_cells. A possible solution is:

As for having that kind of matching logic, the problem is we don't know the config field names at the time we read the environment. We could potentially delay reading until we are deserializing to a users struct but we would still only know about the current field being read, not every possible field, due to how serde works.

epage avatar Jun 30 '25 15:06 epage