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