diff --git a/cmd/members/invite.go b/cmd/members/invite.go deleted file mode 100644 index 9c3b7dd4..00000000 --- a/cmd/members/invite.go +++ /dev/null @@ -1,77 +0,0 @@ -package members - -import ( - "context" - "fmt" - - "github.com/spf13/cobra" - "github.com/spf13/viper" - - "ldcli/cmd/cliflags" - "ldcli/cmd/validators" - "ldcli/internal/errors" - "ldcli/internal/members" - "ldcli/internal/output" -) - -func NewInviteCmd(client members.Client) (*cobra.Command, error) { - cmd := &cobra.Command{ - Args: validators.Validate(), - Long: "Create new members and send them an invitation email", - RunE: runInvite(client), - Short: "Invite new members", - Use: "invite", - } - - cmd.Flags().StringSliceP(cliflags.EmailsFlag, "e", []string{}, "A comma separated list of emails") - err := cmd.MarkFlagRequired(cliflags.EmailsFlag) - if err != nil { - return nil, err - } - err = viper.BindPFlag(cliflags.EmailsFlag, cmd.Flags().Lookup(cliflags.EmailsFlag)) - if err != nil { - return nil, err - } - - cmd.Flags().StringP( - cliflags.RoleFlag, - "r", - "reader", - "Built-in role for the member - one of reader, writer, or admin", - ) - err = viper.BindPFlag(cliflags.RoleFlag, cmd.Flags().Lookup(cliflags.RoleFlag)) - if err != nil { - return nil, err - } - - return cmd, nil -} - -func runInvite(client members.Client) func(*cobra.Command, []string) error { - return func(cmd *cobra.Command, args []string) error { - emails := viper.GetStringSlice(cliflags.EmailsFlag) - memberInputs := make([]members.MemberInput, 0, len(emails)) - for _, e := range emails { - role := viper.GetString(cliflags.RoleFlag) - memberInputs = append(memberInputs, members.MemberInput{Email: e, Role: role}) - } - response, err := client.Create( - context.Background(), - viper.GetString(cliflags.AccessTokenFlag), - viper.GetString(cliflags.BaseURIFlag), - memberInputs, - ) - if err != nil { - return errors.NewError(output.CmdOutputError(viper.GetString(cliflags.OutputFlag), err)) - } - - output, err := output.CmdOutput("create", viper.GetString(cliflags.OutputFlag), response) - if err != nil { - return errors.NewError(err.Error()) - } - - fmt.Fprintf(cmd.OutOrStdout(), output+"\n") - - return nil - } -} diff --git a/cmd/members/invite_test.go b/cmd/members/invite_test.go deleted file mode 100644 index 9c85f382..00000000 --- a/cmd/members/invite_test.go +++ /dev/null @@ -1,251 +0,0 @@ -package members_test - -import ( - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "ldcli/cmd" - "ldcli/internal/analytics" - "ldcli/internal/errors" - "ldcli/internal/members" -) - -func TestInvite(t *testing.T) { - t.Skip("to be replaced with generated cmds tests") - errorHelp := ". See `ldcli members invite --help` for supported flags and usage." - readerRole := "reader" - mockArgs := []interface{}{ - "testAccessToken", - "http://test.com", - []members.MemberInput{ - {Email: "testemail1@test.com", Role: readerRole}, - {Email: "testemail2@test.com", Role: readerRole}, - }, - } - - t.Run("with valid flags calls members API", func(t *testing.T) { - client := members.MockClient{} - client. - On("Create", mockArgs...). - Return([]byte(cmd.StubbedSuccessResponse), nil) - clients := cmd.APIClients{ - MembersClient: &client, - } - args := []string{ - "members", - "invite", - "--access-token", "testAccessToken", - "--base-uri", "http://test.com", - "--output", "json", - "-e", `testemail1@test.com,testemail2@test.com`, - } - - output, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args) - - require.NoError(t, err) - assert.JSONEq(t, cmd.StubbedSuccessResponse, string(output)) - }) - - t.Run("with valid flags from environment variables calls API", func(t *testing.T) { - teardownTest := cmd.SetupTestEnvVars(t) - defer teardownTest(t) - client := members.MockClient{} - client. - On("Update", mockArgs...). - Return([]byte(cmd.StubbedSuccessResponse), nil) - client. - On("Create", mockArgs...). - Return([]byte(cmd.StubbedSuccessResponse), nil) - clients := cmd.APIClients{ - MembersClient: &client, - } - args := []string{ - "members", - "invite", - "--output", "json", - "-e", `testemail1@test.com,testemail2@test.com`, - } - - output, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args) - - require.NoError(t, err) - assert.JSONEq(t, cmd.StubbedSuccessResponse, string(output)) - }) - - t.Run("with an error response is an error", func(t *testing.T) { - client := members.MockClient{} - client. - On("Create", mockArgs...). - Return([]byte(`{}`), errors.NewError(`{"message": "An error"}`)) - clients := cmd.APIClients{ - MembersClient: &client, - } - args := []string{ - "members", - "invite", - "--access-token", "testAccessToken", - "--base-uri", "http://test.com", - "-e", `testemail1@test.com,testemail2@test.com`, - } - - _, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args) - - require.EqualError(t, err, "An error") - }) - - t.Run("with missing required flags is an error", func(t *testing.T) { - clients := cmd.APIClients{ - MembersClient: &members.MockClient{}, - } - args := []string{ - "members", - "invite", - } - - _, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args) - - assert.EqualError(t, err, `required flag(s) "access-token", "emails" not set`+cmd.ExtraErrorHelp("members", "invite")) - }) - - t.Run("with invalid base-uri is an error", func(t *testing.T) { - clients := cmd.APIClients{ - MembersClient: &members.MockClient{}, - } - args := []string{ - "members", - "invite", - "--base-uri", "invalid", - } - - _, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args) - - assert.EqualError(t, err, "base-uri is invalid"+errorHelp) - }) - - t.Run("will track analytics for CLI Command Run event", func(t *testing.T) { - tracker := analytics.MockedTracker( - "members", - "invite", - []string{ - "access-token", - "base-uri", - "emails", - "output", - }, analytics.SUCCESS) - - client := members.MockClient{} - client. - On("Create", mockArgs...). - Return([]byte(cmd.StubbedSuccessResponse), nil) - clients := cmd.APIClients{ - MembersClient: &client, - } - args := []string{ - "members", - "invite", - "--access-token", "testAccessToken", - "--base-uri", "http://test.com", - "--output", "json", - "-e", - `testemail1@test.com,testemail2@test.com`, - } - - _, err := cmd.CallCmd(t, clients, tracker, args) - require.NoError(t, err) - }) -} - -func TestInviteWithOptionalRole(t *testing.T) { - t.Skip("to be replaced with generated cmds tests") - writerRole := "writer" - mockArgs := []interface{}{ - "testAccessToken", - "http://test.com", - []members.MemberInput{ - {Email: "testemail1@test.com", Role: writerRole}, - {Email: "testemail2@test.com", Role: writerRole}, - }, - } - - t.Run("with valid optional long form flag calls members API", func(t *testing.T) { - client := members.MockClient{} - client. - On("Create", mockArgs...). - Return([]byte(cmd.StubbedSuccessResponse), nil) - clients := cmd.APIClients{ - MembersClient: &client, - } - args := []string{ - "members", - "invite", - "--access-token", "testAccessToken", - "--base-uri", "http://test.com", - "--output", "json", - "-e", `testemail1@test.com,testemail2@test.com`, - "--role", "writer", - } - - output, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args) - - require.NoError(t, err) - assert.JSONEq(t, cmd.StubbedSuccessResponse, string(output)) - }) - - t.Run("with valid optional short form flag calls members API", func(t *testing.T) { - client := members.MockClient{} - client. - On("Create", mockArgs...). - Return([]byte(cmd.StubbedSuccessResponse), nil) - clients := cmd.APIClients{ - MembersClient: &client, - } - args := []string{ - "members", - "invite", - "--access-token", "testAccessToken", - "--base-uri", "http://test.com", - "--output", "json", - "-e", `testemail1@test.com,testemail2@test.com`, - "-r", "writer", - } - - output, err := cmd.CallCmd(t, clients, &analytics.NoopClient{}, args) - - require.NoError(t, err) - assert.JSONEq(t, cmd.StubbedSuccessResponse, string(output)) - }) - - t.Run("will track analytics for CLI Command Run event", func(t *testing.T) { - tracker := analytics.MockedTracker( - "members", - "invite", - []string{ - "access-token", - "base-uri", - "emails", - "output", - "role", - }, analytics.SUCCESS) - - client := members.MockClient{} - client. - On("Create", mockArgs...). - Return([]byte(cmd.StubbedSuccessResponse), nil) - clients := cmd.APIClients{ - MembersClient: &client, - } - args := []string{ - "members", "invite", - "--access-token", "testAccessToken", - "--base-uri", "http://test.com", - "--output", "json", - "-e", `testemail1@test.com,testemail2@test.com`, - "--role", "writer", - } - - _, err := cmd.CallCmd(t, clients, tracker, args) - require.NoError(t, err) - }) -} diff --git a/cmd/members/members.go b/cmd/members/members.go deleted file mode 100644 index af5072e6..00000000 --- a/cmd/members/members.go +++ /dev/null @@ -1,37 +0,0 @@ -package members - -import ( - "github.com/spf13/cobra" - "github.com/spf13/viper" - - cmdAnalytics "ldcli/cmd/analytics" - "ldcli/cmd/cliflags" - "ldcli/internal/analytics" - "ldcli/internal/members" -) - -func NewMembersCmd(analyticsTracker analytics.Tracker, client members.Client) (*cobra.Command, error) { - cmd := &cobra.Command{ - Use: "members", - Short: "Make requests (list, create, etc.) on members", - Long: "Make requests (list, create, etc.) on members", - PersistentPreRun: func(cmd *cobra.Command, args []string) { - analyticsTracker.SendCommandRunEvent( - viper.GetString(cliflags.AccessTokenFlag), - viper.GetString(cliflags.BaseURIFlag), - viper.GetBool(cliflags.AnalyticsOptOut), - cmdAnalytics.CmdRunEventProperties(cmd, "members", nil), - ) - }, - } - - inviteCmd, err := NewInviteCmd(client) - if err != nil { - return nil, err - } - - cmd.AddCommand(inviteCmd) - - return cmd, nil - -} diff --git a/cmd/root.go b/cmd/root.go index 634529a4..55798310 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,7 +4,6 @@ package cmd import ( "fmt" - mbrscmd "ldcli/cmd/members" "log" "os" "strconv" @@ -139,14 +138,8 @@ func NewRootCommand( return nil, err } - membersCmd, err := mbrscmd.NewMembersCmd(analyticsTracker, clients.MembersClient) - if err != nil { - return nil, err - } - cmd.AddCommand(configcmd.NewConfigCmd(analyticsTracker)) cmd.AddCommand(NewQuickStartCmd(analyticsTracker, clients.EnvironmentsClient, clients.FlagsClient)) - cmd.AddCommand(membersCmd) resourcecmd.AddAllResourceCmds(cmd, clients.ResourcesClient, analyticsTracker)