From 504591e7d69ba37aed48393c467d32a998a76751 Mon Sep 17 00:00:00 2001 From: George Goldberg Date: Mon, 10 Sep 2018 12:42:15 +0100 Subject: MM-11882: Remove all users from a channel via CLI. (#9381) --- cmd/mattermost/commands/channel.go | 39 ++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'cmd') diff --git a/cmd/mattermost/commands/channel.go b/cmd/mattermost/commands/channel.go index c57a0702d..270ed6586 100644 --- a/cmd/mattermost/commands/channel.go +++ b/cmd/mattermost/commands/channel.go @@ -35,11 +35,12 @@ var ChannelRenameCmd = &cobra.Command{ } var RemoveChannelUsersCmd = &cobra.Command{ - Use: "remove [channel] [users]", - Short: "Remove users from channel", - Long: "Remove some users from channel", - Example: " channel remove myteam:mychannel user@example.com username", - RunE: removeChannelUsersCmdF, + Use: "remove [channel] [users]", + Short: "Remove users from channel", + Long: "Remove some users from channel", + Example: ` channel remove myteam:mychannel user@example.com username + channel remove myteam:mychannel --all-users`, + RunE: removeChannelUsersCmdF, } var AddChannelUsersCmd = &cobra.Command{ @@ -125,6 +126,8 @@ func init() { ChannelRenameCmd.Flags().String("display_name", "", "Channel Display Name") + RemoveChannelUsersCmd.Flags().Bool("all-users", false, "Remove all users from the indicated channel.") + ChannelCmd.AddCommand( ChannelCreateCmd, RemoveChannelUsersCmd, @@ -198,8 +201,14 @@ func removeChannelUsersCmdF(command *cobra.Command, args []string) error { } defer a.Shutdown() - if len(args) < 2 { - return errors.New("Not enough arguments.") + allUsers, _ := command.Flags().GetBool("all-users") + + if allUsers && len(args) != 1 { + return errors.New("individual users must not be specified in conjunction with the --all-users flag") + } + + if !allUsers && len(args) < 2 { + return errors.New("you must specify some users to remove from the channel, or use the --all-users flag to remove them all") } channel := getChannelFromChannelArg(a, args[0]) @@ -207,9 +216,13 @@ func removeChannelUsersCmdF(command *cobra.Command, args []string) error { return errors.New("Unable to find channel '" + args[0] + "'") } - users := getUsersFromUserArgs(a, args[1:]) - for i, user := range users { - removeUserFromChannel(a, channel, user, args[i+1]) + if allUsers { + removeAllUsersFromChannel(a, channel) + } else { + users := getUsersFromUserArgs(a, args[1:]) + for i, user := range users { + removeUserFromChannel(a, channel, user, args[i+1]) + } } return nil @@ -225,6 +238,12 @@ func removeUserFromChannel(a *app.App, channel *model.Channel, user *model.User, } } +func removeAllUsersFromChannel(a *app.App, channel *model.Channel) { + if result := <-a.Srv.Store.Channel().PermanentDeleteMembersByChannel(channel.Id); result.Err != nil { + CommandPrintErrorln("Unable to remove all users from " + channel.Name + ". Error: " + result.Err.Error()) + } +} + func addChannelUsersCmdF(command *cobra.Command, args []string) error { a, err := InitDBCommandContextCobra(command) if err != nil { -- cgit v1.2.3-1-g7c22