feat: support env vars#171
Conversation
| mockArgs := []interface{}{ | ||
| "testAccessToken", | ||
| "https://app.launchdarkly.com", | ||
| "http://test.com", |
There was a problem hiding this comment.
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.
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.
"projects" was copy/pasted into all these tests.
| SilenceUsage: true, | ||
| } | ||
|
|
||
| viper.SetEnvPrefix("LD") |
There was a problem hiding this comment.
This is the main change.
| // 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.
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.
sunnyguduru
left a comment
There was a problem hiding this comment.
Tried it out locally and it worked for me. Tests look good too
Adds support for environment variables defined as
LD_{key}wherekeyis 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.