From cba33137d25336cd5a5bcfd1d68695b584714f56 Mon Sep 17 00:00:00 2001 From: Ashutosh Kumar Date: Fri, 5 Oct 2018 11:42:03 +0530 Subject: MM-12357: Add CLI command "team list" (#9531) * (feat:command list)add command list for teams Signed-off-by: sonasingh46 * address review comments Signed-off-by: sonasingh46 * address review comments Signed-off-by: sonasingh46 --- cmd/mattermost/commands/command.go | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'cmd') diff --git a/cmd/mattermost/commands/command.go b/cmd/mattermost/commands/command.go index 147cd823c..aee2eb290 100644 --- a/cmd/mattermost/commands/command.go +++ b/cmd/mattermost/commands/command.go @@ -6,6 +6,7 @@ package commands import ( "errors" + "fmt" "github.com/mattermost/mattermost-server/app" "github.com/mattermost/mattermost-server/model" "github.com/spf13/cobra" @@ -24,9 +25,18 @@ var CommandMoveCmd = &cobra.Command{ RunE: moveCommandCmdF, } +var CommandListCmd = &cobra.Command{ + Use: "list", + Short: "List all commands on specified teams.", + Long: `List all commands on specified teams.`, + Example: ` command list myteam`, + RunE: listCommandCmdF, +} + func init() { CommandCmd.AddCommand( CommandMoveCmd, + CommandListCmd, ) RootCmd.AddCommand(CommandCmd) } @@ -67,3 +77,40 @@ func moveCommandCmdF(command *cobra.Command, args []string) error { func moveCommand(a *app.App, team *model.Team, command *model.Command) *model.AppError { return a.MoveCommand(team, command) } + +func listCommandCmdF(command *cobra.Command, args []string) error { + a, err := InitDBCommandContextCobra(command) + if err != nil { + return err + } + defer a.Shutdown() + + var teams []*model.Team + if len(args) < 1 { + teamList, err := a.GetAllTeams() + if err != nil { + return err + } + teams = teamList + } else { + teams = getTeamsFromTeamArgs(a, args) + } + + for i, team := range teams { + if team == nil { + CommandPrintErrorln("Unable to find team '" + args[i] + "'") + continue + } + result := <-a.Srv.Store.Command().GetByTeam(team.Id) + if result.Err != nil { + CommandPrintErrorln("Unable to list commands for '" + args[i] + "'") + continue + } + commands := result.Data.([]*model.Command) + for _, command := range commands { + commandListItem := fmt.Sprintf("%s: %s (team: %s)", command.Id, command.DisplayName, team.Name) + CommandPrettyPrintln(commandListItem) + } + } + return nil +} -- cgit v1.2.3-1-g7c22