-
Notifications
You must be signed in to change notification settings - Fork 13
feat: create command to get an environment #96
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package environments | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "ldcli/internal/environments" | ||
| ) | ||
|
|
||
| func NewEnvironmentsCmd(client environments.Client) (*cobra.Command, error) { | ||
| cmd := &cobra.Command{ | ||
| Use: "environments", | ||
| Short: "Make requests (list, create, etc.) on environments", | ||
| Long: "Make requests (list, create, etc.) on environments", | ||
| } | ||
|
|
||
| getCmd, err := NewGetCmd(client) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| cmd.AddCommand(getCmd) | ||
|
|
||
| return cmd, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package environments | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
|
|
||
| "ldcli/cmd/cliflags" | ||
| "ldcli/cmd/validators" | ||
| "ldcli/internal/environments" | ||
| ) | ||
|
|
||
| func NewGetCmd(client environments.Client) (*cobra.Command, error) { | ||
| cmd := &cobra.Command{ | ||
| Args: validators.Validate(), | ||
| Long: "Return an environment", | ||
| RunE: runGet(client), | ||
| Short: "Return an environment", | ||
| Use: "get", | ||
| } | ||
|
|
||
| cmd.Flags().StringP(cliflags.EnvironmentFlag, "e", "", "Environment key") | ||
| err := cmd.MarkFlagRequired(cliflags.EnvironmentFlag) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| err = viper.BindPFlag(cliflags.EnvironmentFlag, cmd.Flags().Lookup(cliflags.EnvironmentFlag)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| cmd.Flags().StringP(cliflags.ProjectFlag, "p", "", "Project key") | ||
| err = cmd.MarkFlagRequired(cliflags.ProjectFlag) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| err = viper.BindPFlag(cliflags.ProjectFlag, cmd.Flags().Lookup(cliflags.ProjectFlag)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return cmd, nil | ||
| } | ||
|
|
||
| func runGet(client environments.Client) func(*cobra.Command, []string) error { | ||
| return func(cmd *cobra.Command, args []string) error { | ||
| _ = viper.BindPFlag(cliflags.EnvironmentFlag, cmd.Flags().Lookup(cliflags.EnvironmentFlag)) | ||
| _ = viper.BindPFlag(cliflags.ProjectFlag, cmd.Flags().Lookup(cliflags.ProjectFlag)) | ||
|
|
||
| response, err := client.Get( | ||
| context.Background(), | ||
| viper.GetString(cliflags.AccessTokenFlag), | ||
| viper.GetString(cliflags.BaseURIFlag), | ||
| viper.GetString(cliflags.EnvironmentFlag), | ||
| viper.GetString(cliflags.ProjectFlag), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fmt.Fprintf(cmd.OutOrStdout(), string(response)+"\n") | ||
|
|
||
| return nil | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package environments_test | ||
|
|
||
| import ( | ||
| "ldcli/cmd" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "ldcli/internal/environments" | ||
| "ldcli/internal/errors" | ||
| ) | ||
|
|
||
| func TestGet(t *testing.T) { | ||
| errorHelp := ". See `ldcli environments get --help` for supported flags and usage." | ||
| mockArgs := []interface{}{ | ||
| "testAccessToken", | ||
| "https://app.launchdarkly.com", | ||
| "test-env", | ||
| "test-proj", | ||
| } | ||
| t.Run("with valid environments calls projects API", func(t *testing.T) { | ||
| client := environments.MockClient{} | ||
| client. | ||
| On("Get", mockArgs...). | ||
| Return([]byte(cmd.ValidResponse), nil) | ||
| args := []string{ | ||
| "environments", "get", | ||
| "--access-token", "testAccessToken", | ||
| "--environment", "test-env", | ||
| "--project", "test-proj", | ||
| } | ||
|
|
||
| output, err := cmd.CallCmd(t, &client, nil, nil, nil, args) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.JSONEq(t, `{"valid": true}`, string(output)) | ||
| }) | ||
|
|
||
| t.Run("with an error response is an error", func(t *testing.T) { | ||
| client := environments.MockClient{} | ||
| client. | ||
| On("Get", mockArgs...). | ||
| Return([]byte(`{}`), errors.NewError("An error")) | ||
| args := []string{ | ||
| "environments", "get", | ||
| "--access-token", "testAccessToken", | ||
| "--environment", "test-env", | ||
| "--project", "test-proj", | ||
| } | ||
|
|
||
| _, err := cmd.CallCmd(t, &client, nil, nil, nil, args) | ||
|
|
||
| require.EqualError(t, err, "An error") | ||
| }) | ||
|
|
||
| t.Run("with missing required environments is an error", func(t *testing.T) { | ||
| args := []string{ | ||
| "environments", "get", | ||
| } | ||
|
|
||
| _, err := cmd.CallCmd(t, &environments.MockClient{}, nil, nil, nil, args) | ||
|
|
||
| assert.EqualError(t, err, `required flag(s) "access-token", "environment", "project" not set`+errorHelp) | ||
| }) | ||
|
|
||
| t.Run("with missing short flag value is an error", func(t *testing.T) { | ||
| args := []string{ | ||
| "environments", "get", | ||
| "-e", | ||
| } | ||
|
|
||
| _, err := cmd.CallCmd(t, &environments.MockClient{}, nil, nil, nil, args) | ||
|
|
||
| assert.EqualError(t, err, `flag needs an argument: 'e' in -e`) | ||
| }) | ||
|
|
||
| t.Run("with missing long flag value is an error", func(t *testing.T) { | ||
| args := []string{ | ||
| "environments", "get", | ||
| "--environment", | ||
| } | ||
|
|
||
| _, err := cmd.CallCmd(t, &environments.MockClient{}, nil, nil, nil, args) | ||
|
|
||
| assert.EqualError(t, err, `flag needs an argument: --environment`) | ||
| }) | ||
|
|
||
| t.Run("with invalid base-uri is an error", func(t *testing.T) { | ||
| args := []string{ | ||
| "environments", "get", | ||
| "--access-token", "testAccessToken", | ||
| "--base-uri", "invalid", | ||
| "--environment", "test-env", | ||
| "--project", "test-proj", | ||
| } | ||
|
|
||
| _, err := cmd.CallCmd(t, &environments.MockClient{}, nil, nil, nil, args) | ||
|
|
||
| assert.EqualError(t, err, "base-uri is invalid"+errorHelp) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 one and sorted.