Skip to content

Fix context.(Global)IsSet to respect environment variables - #502

Merged
meatballhat merged 1 commit into
masterfrom
fix-is-set-for-env
Aug 1, 2016
Merged

meatballhat merged 1 commit into
masterfrom
fix-is-set-for-env

Conversation

@jszwedko

Copy link
Copy Markdown
Contributor

This appeared to be the least messy approach to hack in support for
IsSet also checking environment variables to see if a particular
cli.Flag was set without making backwards incompatible changes to the
interface.

I intend to fix this more properly in v2, probably by adding another
method to the cli.Flag interface to push the responsibility down as it
occurred to me that it was really the Flags themselves that offer
support for configuration via the environment as opposed to the
context or other supporting structures. This opens the door for the
anything implementing the Flag interface to have additional sources of
input while still supporting context.IsSet.

This appeared to be the least messy approach to hack in support for
IsSet also checking environment variables to see if a particular
cli.Flag was set without making backwards incompatible changes to the
interface.

I intend to fix this more properly in v2, probably by adding another
method to the cli.Flag interface to push the responsibility down as it
occurred to me that it was really the `Flag`s themselves that offer
support for configuration via the environment as opposed to the
`context` or other supporting structures. This opens the door for the
anything implementing the `Flag` interface to have additional sources of
input while still supporting `context.IsSet`.
@jszwedko

Copy link
Copy Markdown
Contributor Author

Fixes #294

Comment thread context.go
return
}

envVars := reflect.ValueOf(f).FieldByName("EnvVar").String()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given that this is a context level operation in v1, I think the lookup of the EnvVar field should be more defensive since it's not a part of the Flag interface.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

During my testing, I discovered that you just end up with "" in the case that EnvVar doesn't exist on the struct.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hot diggety! Ignore me then 😸

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually, scratch that, you end up with <invalid Value>. I must have made a mistake before, will update.

@meatballhat

Copy link
Copy Markdown
Member

LGTM other than that one lil note :shipit:

@meatballhat
meatballhat merged commit d604690 into master Aug 1, 2016
@meatballhat
meatballhat deleted the fix-is-set-for-env branch August 1, 2016 03:06
@jszwedko

jszwedko commented Aug 1, 2016

Copy link
Copy Markdown
Contributor Author

Haha, too quick for me, will update on master for the EnvVar missing case.

@jszwedko

jszwedko commented Aug 1, 2016

Copy link
Copy Markdown
Contributor Author

Fixed in 168c954

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.

2 participants