-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support env vars #171
Conversation
@@ -15,18 +15,39 @@ func TestGet(t *testing.T) { | |||
errorHelp := ". See `ldcli environments get --help` for supported flags and usage." | |||
mockArgs := []interface{}{ | |||
"testAccessToken", | |||
"https://app.launchdarkly.com", | |||
"http://test.com", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this for consistency with the other tests so we can use the test helper.
assert.JSONEq(t, `{"valid": true}`, string(output)) | ||
}) | ||
|
||
t.Run("with valid flags from environment variables calls API", func(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to refactor this since it's always the same for each command.
"test-env", | ||
"test-proj", | ||
} | ||
t.Run("with valid environments calls projects API", func(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"projects" was copy/pasted into all these tests.
// Handle errors differently based on type. | ||
// We don't want to show the usage if the user has the right structure but invalid data such as | ||
// the wrong key. | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
} | ||
|
||
viper.SetEnvPrefix("LD") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the main change.
@@ -15,6 +16,7 @@ import ( | |||
// Validate is a validator for commands to print an error when the user input is invalid. | |||
func Validate() cobra.PositionalArgs { | |||
return func(cmd *cobra.Command, args []string) error { | |||
rebindFlags(cmd, cmd.ValidArgs) // rebind flags before validating them below |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate
gets called before any of the callback methods on a command, so we have to rebind this early to make sure the flags below are set with what's in viper. Otherwise, cobra doesn't know that the flags were set from environment variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried it out locally and it worked for me. Tests look good too
Adds support for environment variables defined as
LD_{key}
wherekey
is all caps.Now you can run commands like this:
Requirements
Related issues
Provide links to any issues in this repository or elsewhere relating to this pull request.
Describe the solution you've provided
Provide a clear and concise description of what you expect to happen.
Describe alternatives you've considered
Provide a clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context about the pull request here.