github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/cmd/mattermost/commands/command.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package commands 5 6 import ( 7 "errors" 8 9 "github.com/mattermost/mattermost-server/app" 10 "github.com/mattermost/mattermost-server/model" 11 "github.com/spf13/cobra" 12 ) 13 14 var CommandCmd = &cobra.Command{ 15 Use: "command", 16 Short: "Management of slash commands", 17 } 18 19 var CommandMoveCmd = &cobra.Command{ 20 Use: "move", 21 Short: "Move a slash command to a different team", 22 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.`, 23 Example: ` command move newteam oldteam:command`, 24 RunE: moveCommandCmdF, 25 } 26 27 func init() { 28 CommandCmd.AddCommand( 29 CommandMoveCmd, 30 ) 31 RootCmd.AddCommand(CommandCmd) 32 } 33 34 func moveCommandCmdF(command *cobra.Command, args []string) error { 35 a, err := InitDBCommandContextCobra(command) 36 if err != nil { 37 return err 38 } 39 defer a.Shutdown() 40 41 if len(args) < 2 { 42 return errors.New("Enter the destination team and at least one comamnd to move.") 43 } 44 45 team := getTeamFromTeamArg(a, args[0]) 46 if team == nil { 47 return errors.New("Unable to find destination team '" + args[0] + "'") 48 } 49 50 commands := getCommandsFromCommandArgs(a, args[1:]) 51 CommandPrintErrorln(commands) 52 for i, command := range commands { 53 if command == nil { 54 CommandPrintErrorln("Unable to find command '" + args[i+1] + "'") 55 continue 56 } 57 if err := moveCommand(a, team, command); err != nil { 58 CommandPrintErrorln("Unable to move command '" + command.Trigger + "' error: " + err.Error()) 59 } else { 60 CommandPrettyPrintln("Moved command '" + command.Trigger + "'") 61 } 62 } 63 64 return nil 65 } 66 67 func moveCommand(a *app.App, team *model.Team, command *model.Command) *model.AppError { 68 return a.MoveCommand(team, command) 69 }