MM-12358 Create CLI command "command modify" (#10538)

* Added ability to modify a command using the CLI

* Added ability to modify a command using the CLI

* Added TestModifyCommand to command_test.go and unit tests for new modify command

* Added ability to modify a command using the CLI

* Added TestModifyCommand to command_test.go and unit tests for new modify command
Этот коммит содержится в:
James Vasky
2019-04-09 05:27:15 -04:00
коммит произвёл George Goldberg
родитель 40b97a1a4f
Коммит 1a102e13c8
2 изменённых файлов: 344 добавлений и 0 удалений

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

@@ -62,6 +62,15 @@ var CommandDeleteCmd = &cobra.Command{
RunE: deleteCommandCmdF,
}
var CommandModifyCmd = &cobra.Command{
Use: "modify",
Short: "Modify a slash command",
Long: `Modify a slash command. Commands can be specified by command ID.`,
Example: ` command modify commandID`,
Args: cobra.MinimumNArgs(1),
RunE: modifyCommandCmdF,
}
func init() {
CommandCreateCmd.Flags().String("title", "", "Command Title")
CommandCreateCmd.Flags().String("description", "", "Command Description")
@@ -78,12 +87,25 @@ func init() {
CommandCreateCmd.Flags().String("autocompleteHint", "", "Command Arguments displayed as help in autocomplete list")
CommandCreateCmd.Flags().Bool("post", false, "Use POST method for Callback URL")
CommandModifyCmd.Flags().String("title", "", "Command Title")
CommandModifyCmd.Flags().String("description", "", "Command Description")
CommandModifyCmd.Flags().String("trigger-word", "", "Command Trigger Word")
CommandModifyCmd.Flags().String("url", "", "Command Callback URL")
CommandModifyCmd.Flags().String("creator", "", "Command Creator's Username")
CommandModifyCmd.Flags().String("response-username", "", "Command Response Username")
CommandModifyCmd.Flags().String("icon", "", "Command Icon URL")
CommandModifyCmd.Flags().Bool("autocomplete", false, "Show Command in autocomplete list")
CommandModifyCmd.Flags().String("autocompleteDesc", "", "Short Command Description for autocomplete list")
CommandModifyCmd.Flags().String("autocompleteHint", "", "Command Arguments displayed as help in autocomplete list")
CommandModifyCmd.Flags().Bool("post", false, "Use POST method for Callback URL")
CommandCmd.AddCommand(
CommandCreateCmd,
CommandShowCmd,
CommandMoveCmd,
CommandListCmd,
CommandDeleteCmd,
CommandModifyCmd,
)
RootCmd.AddCommand(CommandCmd)
}
@@ -268,3 +290,97 @@ func deleteCommandCmdF(command *cobra.Command, args []string) error {
CommandPrettyPrintln("Deleted command '" + slashCommand.Id + "' (" + slashCommand.DisplayName + ")")
return nil
}
func modifyCommandCmdF(command *cobra.Command, args []string) error {
a, err := InitDBCommandContextCobra(command)
if err != nil {
return err
}
defer a.Shutdown()
oldCommand := getCommandFromCommandArg(a, args[0])
if oldCommand == nil {
command.SilenceUsage = true
return errors.New("Unable to find command '" + args[0] + "'")
}
modifiedCommand := oldCommand
// get creator user
creator, _ := command.Flags().GetString("creator")
if creator != "" {
user := getUserFromUserArg(a, creator)
if user == nil {
return errors.New("unable to find user '" + creator + "'")
}
// check if creator has permission to create slash commands
if !a.HasPermissionToTeam(user.Id, modifiedCommand.TeamId, model.PERMISSION_MANAGE_SLASH_COMMANDS) {
return errors.New("the creator must be a user who has permissions to manage slash commands")
}
modifiedCommand.CreatorId = user.Id
}
title, _ := command.Flags().GetString("title")
if title != "" {
modifiedCommand.DisplayName = title
}
description, _ := command.Flags().GetString("description")
if description != "" {
modifiedCommand.Description = description
}
trigger, _ := command.Flags().GetString("trigger-word")
if trigger != "" {
if strings.HasPrefix(trigger, "/") {
return errors.New("a trigger word cannot begin with a /")
}
if strings.Contains(trigger, " ") {
return errors.New("a trigger word must not contain spaces")
}
modifiedCommand.Trigger = trigger
}
url, _ := command.Flags().GetString("url")
if url != "" {
modifiedCommand.URL = url
}
responseUsername, _ := command.Flags().GetString("response-username")
if responseUsername != "" {
modifiedCommand.Username = responseUsername
}
icon, _ := command.Flags().GetString("icon")
if icon != "" {
modifiedCommand.IconURL = icon
}
autocomplete, _ := command.Flags().GetBool("autocomplete")
modifiedCommand.AutoComplete = autocomplete
autocompleteDesc, _ := command.Flags().GetString("autocompleteDesc")
if autocompleteDesc != "" {
modifiedCommand.AutoCompleteDesc = autocompleteDesc
}
autocompleteHint, _ := command.Flags().GetString("autocompleteHint")
if autocompleteHint != "" {
modifiedCommand.AutoCompleteHint = autocompleteHint
}
post, err := command.Flags().GetBool("post")
method := "P"
if err != nil || post == false {
method = "G"
}
modifiedCommand.Method = method
if _, err := a.UpdateCommand(oldCommand, modifiedCommand); err != nil {
return errors.New("unable to modify command '" + modifiedCommand.DisplayName + "'. " + err.Error())
}
CommandPrettyPrintln("modified command '" + modifiedCommand.DisplayName + "'")
return nil
}