Skip to content

Consider empty environment variables as set - #530

Merged
jszwedko merged 3 commits into
masterfrom
consider-empty-env-vars
Nov 13, 2016
Merged

jszwedko merged 3 commits into
masterfrom
consider-empty-env-vars

Conversation

@jszwedko

@jszwedko jszwedko commented Sep 12, 2016 •

Copy link
Copy Markdown
Contributor

When assigning values to flags (also when interogatting via
context.(Global)IsSet.

Using syscall.Getenv rather than os.LookupEnv in order to support older Golang versions.

For boolean flags, consider empty as false.

One important consideration here is that context.(Global)IsSet() will return
true for flags whose specified environment variable has been set even when
the 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 IsSet would return false even if the variable truly was set by the
environment.

In order to properly support fix considering environment variables with invalid
values (which are currently silently ignored) is to augment the Flag
interface's Apply() method to be able to return an error (which I'd suggest
we do in v2 since we cannot do this here without breaking compatibilty).

Fixes #517

@jszwedko
jszwedko force-pushed the consider-empty-env-vars branch from 497ee04 to 60cf8c7 Compare September 12, 2016 04:15
@jszwedko

Copy link
Copy Markdown
Contributor Author

I have a couple of ideas for poor hacks to make IsSet consistent with the Apply implementations wherein it returns false if the env var was set, but was not parsable (while maintaining backwards compatibility).

I'd be interested in feedback on the current changes though (particularly around bool handling).

/cc @meatballhat @vikstrous

@meatballhat

Copy link
Copy Markdown
Member

LGTM 🤘

@vikstrous

Copy link
Copy Markdown

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.

@jszwedko

Copy link
Copy Markdown
Contributor Author

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, (Global)IsSet will not return true if the environment variables failed to be parsed.

@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 v2 by modifying the existing Flag interface rather than having to have both and reflect.

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 ""
@jszwedko
jszwedko force-pushed the consider-empty-env-vars branch from c4d81ca to b4a64dc Compare November 13, 2016 22:59
@jszwedko
jszwedko merged commit 31b79c9 into master Nov 13, 2016
@jszwedko
jszwedko deleted the consider-empty-env-vars branch November 13, 2016 23:00
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.

c.IsSet returns true for empty env vars

3 participants