PLT-7218: CLI to move slash commands between teams. (#7574)

Этот коммит содержится в:
George Goldberg
2017-10-04 19:08:59 +01:00
коммит произвёл Chris
родитель e16bdf8d1d
Коммит f3fc6d11fa
9 изменённых файлов: 252 добавлений и 1 удалений

69
cmd/platform/command.go Обычный файл
Просмотреть файл

@@ -0,0 +1,69 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package main
import (
"errors"
"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,
}
func init() {
commandCmd.AddCommand(
commandMoveCmd,
)
}
func moveCommandCmdF(cmd *cobra.Command, args []string) error {
a, err := initDBCommandContextCobra(cmd)
if err != nil {
return err
}
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 {
if err := a.MoveCommand(team, command); err != nil {
return err
}
return nil
}

63
cmd/platform/commandargs.go Обычный файл
Просмотреть файл

@@ -0,0 +1,63 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package main
import (
"fmt"
"strings"
"github.com/mattermost/mattermost-server/app"
"github.com/mattermost/mattermost-server/model"
)
const COMMAND_ARGS_SEPARATOR = ":"
func getCommandsFromCommandArgs(a *app.App, commandArgs []string) []*model.Command {
commands := make([]*model.Command, 0, len(commandArgs))
for _, commandArg := range commandArgs {
command := getCommandFromCommandArg(a, commandArg)
commands = append(commands, command)
}
return commands
}
func parseCommandArg(commandArg string) (string, string) {
result := strings.SplitN(commandArg, COMMAND_ARGS_SEPARATOR, 2)
if len(result) == 1 {
return "", commandArg
}
return result[0], result[1]
}
func getCommandFromCommandArg(a *app.App, commandArg string) *model.Command {
teamArg, commandPart := parseCommandArg(commandArg)
if teamArg == "" && commandPart == "" {
return nil
}
var command *model.Command
if teamArg != "" {
team := getTeamFromTeamArg(a, teamArg)
if team == nil {
return nil
}
if result := <-a.Srv.Store.Command().GetByTrigger(team.Id, commandPart); result.Err == nil {
command = result.Data.(*model.Command)
} else {
fmt.Println(result.Err.Error())
}
}
if command == nil {
if result := <-a.Srv.Store.Command().Get(commandPart); result.Err == nil {
command = result.Data.(*model.Command)
}
}
return command
}

Просмотреть файл

@@ -40,7 +40,7 @@ func init() {
resetCmd.Flags().Bool("confirm", false, "Confirm you really want to delete everything and a DB backup has been performed.")
rootCmd.AddCommand(serverCmd, versionCmd, userCmd, teamCmd, licenseCmd, importCmd, resetCmd, channelCmd, rolesCmd, testCmd, ldapCmd, configCmd, jobserverCmd)
rootCmd.AddCommand(serverCmd, versionCmd, userCmd, teamCmd, licenseCmd, importCmd, resetCmd, channelCmd, rolesCmd, testCmd, ldapCmd, configCmd, jobserverCmd, commandCmd)
}
var rootCmd = &cobra.Command{