Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/flags/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestUpdate(t *testing.T) {
"--projKey", "test-proj-key",
}

output, err := cmd.CallCmd(t, &client, nil, args)
output, err := cmd.CallCmd(t, &client, nil, nil, args)

require.NoError(t, err)
assert.JSONEq(t, `{"valid": true}`, string(output))
Expand All @@ -60,7 +60,7 @@ func TestUpdate(t *testing.T) {
"--projKey", "test-proj-key",
}

_, err := cmd.CallCmd(t, &client, nil, args)
_, err := cmd.CallCmd(t, &client, nil, nil, args)

require.EqualError(t, err, "An error")
})
Expand All @@ -70,7 +70,7 @@ func TestUpdate(t *testing.T) {
"flags", "update",
}

_, err := cmd.CallCmd(t, &flags.MockClient{}, nil, args)
_, err := cmd.CallCmd(t, &flags.MockClient{}, nil, nil, args)

assert.EqualError(t, err, `required flag(s) "accessToken", "data", "key", "projKey" not set`)
})
Expand All @@ -84,7 +84,7 @@ func TestUpdate(t *testing.T) {
"--projKey", "test-proj-key",
}

_, err := cmd.CallCmd(t, &flags.MockClient{}, nil, args)
_, err := cmd.CallCmd(t, &flags.MockClient{}, nil, nil, args)

assert.EqualError(t, err, "baseUri is invalid")
})
Expand Down
14 changes: 14 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package client

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 made this singular to hint that there's only one client, but I could change it to clients for consistency if preferred.


import ldapi "github.com/launchdarkly/api-client-go/v14"

// New creates an LD API client. It's not set as a field on the struct because the CLI flags
// are evaluated when running the command, not when executing the program. That means we don't have
// the flag values until the command's RunE method is called.
func New(accessToken string, baseURI string) *ldapi.APIClient {
config := ldapi.NewConfiguration()
config.AddDefaultHeader("Authorization", accessToken)
config.Servers[0].URL = baseURI

return ldapi.NewAPIClient(config)
}
16 changes: 3 additions & 13 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

ldapi "github.com/launchdarkly/api-client-go/v14"

"ldcli/internal/client"
"ldcli/internal/errors"
)

Expand Down Expand Up @@ -37,7 +38,7 @@ func (c FlagsClient) Create(
key,
projectKey string,
) ([]byte, error) {
client := c.client(accessToken, baseURI)
client := client.New(accessToken, baseURI)
post := ldapi.NewFeatureFlagBody(name, key)
flag, _, err := client.FeatureFlagsApi.PostFeatureFlag(ctx, projectKey).FeatureFlagBody(*post).Execute()
if err != nil {
Expand All @@ -61,7 +62,7 @@ func (c FlagsClient) Update(
projKey string,
patch []ldapi.PatchOperation,
) ([]byte, error) {
client := c.client(accessToken, baseURI)
client := client.New(accessToken, baseURI)
flag, _, err := client.FeatureFlagsApi.
PatchFeatureFlag(ctx, projKey, key).
PatchWithComment(*ldapi.NewPatchWithComment(patch)).
Expand All @@ -77,14 +78,3 @@ func (c FlagsClient) Update(

return responseJSON, nil
}

// client creates an LD API client. It's not set as a field on the struct because the CLI flags
// are evaluated when running the command, not when executing the program. That means we don't have
// the flag values until the command's RunE method is called.
func (c FlagsClient) client(accessToken string, baseURI string) *ldapi.APIClient {
config := ldapi.NewConfiguration()
config.AddDefaultHeader("Authorization", accessToken)
config.Servers[0].URL = baseURI

return ldapi.NewAPIClient(config)
}
11 changes: 2 additions & 9 deletions internal/members/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

ldapi "github.com/launchdarkly/api-client-go/v14"

"ldcli/internal/client"
"ldcli/internal/errors"
)

Expand All @@ -20,7 +21,7 @@ func NewClient() Client {
}

func (c MembersClient) Create(ctx context.Context, accessToken, baseURI, email, role string) ([]byte, error) {
client := c.client(accessToken, baseURI)
client := client.New(accessToken, baseURI)
memberForm := ldapi.NewMemberForm{Email: email, Role: &role}
members, _, err := client.AccountMembersApi.PostMembers(ctx).NewMemberForm([]ldapi.NewMemberForm{memberForm}).Execute()
if err != nil {
Expand All @@ -33,11 +34,3 @@ func (c MembersClient) Create(ctx context.Context, accessToken, baseURI, email,

return memberJson, nil
}

func (c MembersClient) client(accessToken string, baseURI string) *ldapi.APIClient {
config := ldapi.NewConfiguration()
config.AddDefaultHeader("Authorization", accessToken)
config.Servers[0].URL = baseURI

return ldapi.NewAPIClient(config)
}
85 changes: 0 additions & 85 deletions internal/members/members_test.go

This file was deleted.

16 changes: 3 additions & 13 deletions internal/projects/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

ldapi "github.com/launchdarkly/api-client-go/v14"

"ldcli/internal/client"
"ldcli/internal/errors"
)

Expand All @@ -29,7 +30,7 @@ func (c ProjectsClient) Create(
name,
key string,
) ([]byte, error) {
client := c.client(accessToken, baseURI)
client := client.New(accessToken, baseURI)
projectPost := ldapi.NewProjectPost(name, key)
project, _, err := client.ProjectsApi.PostProject(ctx).ProjectPost(*projectPost).Execute()
if err != nil {
Expand All @@ -48,7 +49,7 @@ func (c ProjectsClient) List(
accessToken,
baseURI string,
) ([]byte, error) {
client := c.client(accessToken, baseURI)
client := client.New(accessToken, baseURI)
projects, _, err := client.ProjectsApi.
GetProjects(ctx).
Limit(2).
Expand All @@ -64,14 +65,3 @@ func (c ProjectsClient) List(

return projectsJSON, nil
}

// client creates an LD API client. It's not set as a field on the struct because the CLI flags
// are evaluated when running the command, not when executing the program. That means we don't have
// the flag values until the command's RunE method is called.
func (c ProjectsClient) client(accessToken string, baseURI string) *ldapi.APIClient {
config := ldapi.NewConfiguration()
config.AddDefaultHeader("Authorization", accessToken)
config.Servers[0].URL = baseURI

return ldapi.NewAPIClient(config)
}
150 changes: 0 additions & 150 deletions internal/projects/projects_test.go

This file was deleted.