Repository navigation
Consider empty environment variables as set - #530
Conversation
497ee04 to
60cf8c7
Compare
|
I have a couple of ideas for poor hacks to make I'd be interested in feedback on the current changes though (particularly around |
|
LGTM 🤘 |
|
This sounds reasonable. I agree that values that failed to parse shouldn't be considered set. If any variables fail to parse the program shouldn't even get to execute IMO. The behaviour you describe is indeed an improvement and works for my use case. |
|
After discussing briefly with @meatballhat, we decided that it is closer to the original intent of parsing environment variables to error if they are set to unparsable values (rather than silently ignoring). I pushed a second commit, b816af8, which does this and, by consequence, @meatballhat do you mind reviewing the implementation here? It's a bit hacky right now to maintain compatibility, but I think we can slim it down in |
7c3c8e7 to
b7f4c3d
Compare
When assigning values to flags (also when interogatting via `context.(Global)IsSet`. For boolean flags, consider empty as `false`. Using `syscall.Getenv` rather than `os.LookupEnv` in order to support older Golang versions.
Currently cli silently (aside from IntSlice and Int64Slice which oddly printed directly to the error stream) ignores failures that occur when parsing environment variables for their value for flags that define environment variables. Instead, we should propogate up the error to the user. This is accomplished in a backwards compatible manner by adding a new, internal, interface which defines an applyWithError function that all flags here define. In v2, when we can modify the interface, we can drop this secondary interface and modify `Apply` to return an error.
Apparently `Clearenv` in Windows just sets the variables to ""
c4d81ca to
b4a64dc
Compare
When assigning values to flags (also when interogatting via
context.(Global)IsSet.Using
syscall.Getenvrather thanos.LookupEnvin order to support older Golang versions.For boolean flags, consider empty as
false.One important consideration here is that
context.(Global)IsSet()will returntruefor flags whose specified environment variable has been set even whenthe value of that variable fails to be parsed as the type that the flag
requires; however, I think this behavior is better than the previous behavior
where
IsSetwould return false even if the variable truly was set by theenvironment.
In order to properly support fix considering environment variables with invalid
values (which are currently silently ignored) is to augment the
Flaginterface's
Apply()method to be able to return an error (which I'd suggestwe do in
v2since we cannot do this here without breaking compatibilty).Fixes #517