Skip to content

feat: support env vars#171

Merged
dbolson merged 6 commits into
mainfrom
sc-240063/support-env-vars
Apr 15, 2024
Merged

feat: support env vars#171
dbolson merged 6 commits into
mainfrom
sc-240063/support-env-vars

Conversation

@dbolson
Copy link
Copy Markdown
Contributor

@dbolson dbolson commented Apr 12, 2024

Adds support for environment variables defined as LD_{key} where key is all caps.

Now you can run commands like this:

export LD_ACCESS_TOKEN=abc123
export LD_BASE_URI=http://something.com
go run main.go environments get --environment test --project default

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

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.

mockArgs := []interface{}{
"testAccessToken",
"https://app.launchdarkly.com",
"http://test.com",
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.

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) {
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.

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) {
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.

"projects" was copy/pasted into all these tests.

Comment thread cmd/root.go
SilenceUsage: true,
}

viper.SetEnvPrefix("LD")
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.

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
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.

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.

@dbolson dbolson changed the title support env vars feat: support env vars Apr 12, 2024
Copy link
Copy Markdown

@sunnyguduru sunnyguduru left a 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

@dbolson dbolson merged commit b0380ca into main Apr 15, 2024
@dbolson dbolson deleted the sc-240063/support-env-vars branch April 15, 2024 15:13
@sunnyguduru sunnyguduru mentioned this pull request Apr 15, 2024
3 tasks
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