[MM-56854] Add shell completion to mmctl user active|deactivate (#26358)

Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
Ben Schumacher 2024-04-24 10:14:20 +02:00 committed by GitHub
parent 8ba157f284
commit 92a6c6517d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 73 additions and 4 deletions

View File

@ -63,6 +63,7 @@ type Client interface {
GetUser(ctx context.Context, userID, etag string) (*model.User, *model.Response, error) GetUser(ctx context.Context, userID, etag string) (*model.User, *model.Response, error)
GetUserByUsername(ctx context.Context, userName, etag string) (*model.User, *model.Response, error) GetUserByUsername(ctx context.Context, userName, etag string) (*model.User, *model.Response, error)
GetUserByEmail(ctx context.Context, email, etag string) (*model.User, *model.Response, error) GetUserByEmail(ctx context.Context, email, etag string) (*model.User, *model.Response, error)
GetUsersWithCustomQueryParameters(ctx context.Context, page int, perPage int, queryParameters, etag string) ([]*model.User, *model.Response, error)
PermanentDeleteUser(ctx context.Context, userID string) (*model.Response, error) PermanentDeleteUser(ctx context.Context, userID string) (*model.Response, error)
PermanentDeleteAllUsers(ctx context.Context) (*model.Response, error) PermanentDeleteAllUsers(ctx context.Context) (*model.Response, error)
CreateUser(ctx context.Context, user *model.User) (*model.User, *model.Response, error) CreateUser(ctx context.Context, user *model.User) (*model.User, *model.Response, error)

View File

@ -32,8 +32,9 @@ var UserActivateCmd = &cobra.Command{
Long: "Activate users that have been deactivated.", Long: "Activate users that have been deactivated.",
Example: ` user activate user@example.com Example: ` user activate user@example.com
user activate username`, user activate username`,
RunE: withClient(userActivateCmdF), ValidArgsFunction: validateArgsWithClient(userActivateCompletionF),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
RunE: withClient(userActivateCmdF),
} }
var UserDeactivateCmd = &cobra.Command{ var UserDeactivateCmd = &cobra.Command{
@ -42,8 +43,9 @@ var UserDeactivateCmd = &cobra.Command{
Long: "Deactivate users. Deactivated users are immediately logged out of all sessions and are unable to log back in.", Long: "Deactivate users. Deactivated users are immediately logged out of all sessions and are unable to log back in.",
Example: ` user deactivate user@example.com Example: ` user deactivate user@example.com
user deactivate username`, user deactivate username`,
RunE: withClient(userDeactivateCmdF), ValidArgsFunction: validateArgsWithClient(userDeactivateCompletionF),
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
RunE: withClient(userDeactivateCmdF),
} }
var UserCreateCmd = &cobra.Command{ var UserCreateCmd = &cobra.Command{
@ -427,6 +429,15 @@ func userActivateCmdF(c client.Client, command *cobra.Command, args []string) er
return changeUsersActiveStatus(c, args, true) return changeUsersActiveStatus(c, args, true)
} }
func userActivateCompletionF(ctx context.Context, c client.Client, cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return fetchAndComplete(
func(ctx context.Context, c client.Client, page, perPage int) ([]*model.User, *model.Response, error) {
return c.GetUsersWithCustomQueryParameters(ctx, page, perPage, "inactive=true", "")
},
func(u *model.User) []string { return []string{u.Id, u.Username, u.Email} },
)(ctx, c, cmd, args, toComplete)
}
func changeUsersActiveStatus(c client.Client, userArgs []string, active bool) error { func changeUsersActiveStatus(c client.Client, userArgs []string, active bool) error {
var multiErr *multierror.Error var multiErr *multierror.Error
users, err := getUsersFromArgs(c, userArgs) users, err := getUsersFromArgs(c, userArgs)
@ -458,6 +469,15 @@ func userDeactivateCmdF(c client.Client, cmd *cobra.Command, args []string) erro
return changeUsersActiveStatus(c, args, false) return changeUsersActiveStatus(c, args, false)
} }
func userDeactivateCompletionF(ctx context.Context, c client.Client, cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return fetchAndComplete(
func(ctx context.Context, c client.Client, page, perPage int) ([]*model.User, *model.Response, error) {
return c.GetUsersWithCustomQueryParameters(ctx, page, perPage, "active=true", "")
},
func(u *model.User) []string { return []string{u.Id, u.Username, u.Email} },
)(ctx, c, cmd, args, toComplete)
}
func userCreateCmdF(c client.Client, cmd *cobra.Command, args []string) error { func userCreateCmdF(c client.Client, cmd *cobra.Command, args []string) error {
printer.SetSingle(true) printer.SetSingle(true)

View File

@ -179,6 +179,38 @@ func (s *MmctlUnitTestSuite) TestUserActivateCmd() {
s.Require().Equal(fmt.Sprintf("1 error occurred:\n\t* user %s not found\n\n", emailArgs[1]), printer.GetErrorLines()[0]) s.Require().Equal(fmt.Sprintf("1 error occurred:\n\t* user %s not found\n\n", emailArgs[1]), printer.GetErrorLines()[0])
s.Require().Equal(fmt.Sprintf("unable to change activation status of user: %v", mockUser3.Id), printer.GetErrorLines()[1]) s.Require().Equal(fmt.Sprintf("unable to change activation status of user: %v", mockUser3.Id), printer.GetErrorLines()[1])
}) })
s.Run("shell completion", func() {
s.Run("one element matches", func() {
mockUsers := []*model.User{
{
Id: "0_id",
Username: "0_username",
Email: "0_email@example.org",
},
{
Id: "1_id",
Username: "1_username",
Email: "1_email@example.org",
},
{
Id: "2_id",
Username: "2_username",
Email: "2_email@example.org",
},
}
s.client.
EXPECT().
GetUsersWithCustomQueryParameters(context.Background(), 0, DefaultPageSize, "inactive=true", "").
Return(mockUsers, &model.Response{}, nil).
Times(1)
r, dir := userActivateCompletionF(context.Background(), s.client, nil, nil, "1")
s.Equal(cobra.ShellCompDirectiveNoFileComp, dir)
s.Equal([]string{"1_id"}, r)
})
})
} }
func (s *MmctlUnitTestSuite) TestDeactivateUserCmd() { func (s *MmctlUnitTestSuite) TestDeactivateUserCmd() {

View File

@ -1438,6 +1438,22 @@ func (mr *MockClientMockRecorder) GetUsersInTeam(arg0, arg1, arg2, arg3, arg4 in
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUsersInTeam", reflect.TypeOf((*MockClient)(nil).GetUsersInTeam), arg0, arg1, arg2, arg3, arg4) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUsersInTeam", reflect.TypeOf((*MockClient)(nil).GetUsersInTeam), arg0, arg1, arg2, arg3, arg4)
} }
// GetUsersWithCustomQueryParameters mocks base method.
func (m *MockClient) GetUsersWithCustomQueryParameters(arg0 context.Context, arg1, arg2 int, arg3, arg4 string) ([]*model.User, *model.Response, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetUsersWithCustomQueryParameters", arg0, arg1, arg2, arg3, arg4)
ret0, _ := ret[0].([]*model.User)
ret1, _ := ret[1].(*model.Response)
ret2, _ := ret[2].(error)
return ret0, ret1, ret2
}
// GetUsersWithCustomQueryParameters indicates an expected call of GetUsersWithCustomQueryParameters.
func (mr *MockClientMockRecorder) GetUsersWithCustomQueryParameters(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUsersWithCustomQueryParameters", reflect.TypeOf((*MockClient)(nil).GetUsersWithCustomQueryParameters), arg0, arg1, arg2, arg3, arg4)
}
// InstallMarketplacePlugin mocks base method. // InstallMarketplacePlugin mocks base method.
func (m *MockClient) InstallMarketplacePlugin(arg0 context.Context, arg1 *model.InstallMarketplacePluginRequest) (*model.Manifest, *model.Response, error) { func (m *MockClient) InstallMarketplacePlugin(arg0 context.Context, arg1 *model.InstallMarketplacePluginRequest) (*model.Manifest, *model.Response, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()