github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+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  	"github.com/mattermost/mattermost-server/app"
     9  	"github.com/mattermost/mattermost-server/cmd"
    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  	cmd.RootCmd.AddCommand(CommandCmd)
    32  }
    33  
    34  func moveCommandCmdF(command *cobra.Command, args []string) error {
    35  	a, err := cmd.InitDBCommandContextCobra(command)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	if len(args) < 2 {
    41  		return errors.New("Enter the destination team and at least one comamnd to move.")
    42  	}
    43  
    44  	team := getTeamFromTeamArg(a, args[0])
    45  	if team == nil {
    46  		return errors.New("Unable to find destination team '" + args[0] + "'")
    47  	}
    48  
    49  	commands := getCommandsFromCommandArgs(a, args[1:])
    50  	cmd.CommandPrintErrorln(commands)
    51  	for i, command := range commands {
    52  		if command == nil {
    53  			cmd.CommandPrintErrorln("Unable to find command '" + args[i+1] + "'")
    54  			continue
    55  		}
    56  		if err := moveCommand(a, team, command); err != nil {
    57  			cmd.CommandPrintErrorln("Unable to move command '" + command.Trigger + "' error: " + err.Error())
    58  		} else {
    59  			cmd.CommandPrettyPrintln("Moved command '" + command.Trigger + "'")
    60  		}
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  func moveCommand(a *app.App, team *model.Team, command *model.Command) *model.AppError {
    67  	return a.MoveCommand(team, command)
    68  }