Skip to content
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

Merged
merged 6 commits into from
Apr 15, 2024
Merged

feat: support env vars #171

merged 6 commits into from
Apr 15, 2024

Conversation

dbolson
Copy link
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.

@@ -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",
Copy link
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
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
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.

// 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")
Copy link
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.

@@ -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
Copy link
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
Contributor

@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
2 of 3 checks passed
@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