// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package commands import ( "errors" "fmt" "github.com/mattermost/mattermost-server/app" "github.com/mattermost/mattermost-server/model" "github.com/spf13/cobra" ) var CommandCmd = &cobra.Command{ Use: "command", Short: "Management of slash commands", } var CommandMoveCmd = &cobra.Command{ Use: "move", Short: "Move a slash command to a different team", Long: `Move a slash command to a different team. Commands can be specified by [team]:[command-trigger-word]. ie. myteam:trigger or by command ID.`, Example: ` command move newteam oldteam: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) } func moveCommandCmdF(command *cobra.Command, args []string) error { a, err := InitDBCommandContextCobra(command) if err != nil { return err } defer a.Shutdown() if len(args) < 2 { return errors.New("Enter the destination team and at least one comamnd to move.") } team := getTeamFromTeamArg(a, args[0]) if team == nil { return errors.New("Unable to find destination team '" + args[0] + "'") } commands := getCommandsFromCommandArgs(a, args[1:]) CommandPrintErrorln(commands) for i, command := range commands { if command == nil { CommandPrintErrorln("Unable to find command '" + args[i+1] + "'") continue } if err := moveCommand(a, team, command); err != nil { CommandPrintErrorln("Unable to move command '" + command.Trigger + "' error: " + err.Error()) } else { CommandPrettyPrintln("Moved command '" + command.Trigger + "'") } } return nil } 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 }