traefik / paerser

Loads configuration from many sources
Apache License 2.0
54 stars 18 forks source link

feat: make env prefix allow underscores #21

Closed jonasgeiler closed 2 weeks ago

jonasgeiler commented 2 weeks ago

What?

Previously the env prefix could only consist of an alphanumeric string followed by an underscore. I changed to pattern to allow the prefix to have an underscore in it, but not start with an underscore.

Why?

I currently have a root command, server, which uses paerser for all the configuration since I really like the way traefik is configured. The root command loads from env with a SERVER_ prefix. Now I am adding a health check subcommand, server healthcheck, which will run the health check on the server. I also use paerser for this subcommand, but with a different configuration altogether. And now I wanted to use a SERVER_HEALTHCHECK_ prefix for this subcommand to keep the two seperate, but I got the error that the prefix was invalid...

ldez commented 2 weeks ago

Hello,

This will create a regression because the prefix is not trimmed before the replacement of the _.

key := strings.ToLower(prefix[:len(prefix)-1]) + "." + strings.ReplaceAll(strings.ToLower(strings.TrimPrefix(k, prefix)), "_", ".")

But even with the trim and a "custom" build of the key, some side effects can be expected.

In all cases, tests should be added.

jonasgeiler commented 2 weeks ago

I did not know the prefix was parsed this way, and that this would break this parsing method... I'll close the PR for now because I have found a solution I'm okay with (using the same prefix for server healthcheck as server, but only have one sub-struct)