diff options
author | Jesús Espino <jespinog@gmail.com> | 2018-03-07 20:04:18 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-07 20:04:18 +0000 |
commit | b2dd00dd5b83fc7e8b311a55f5a2536e4f3d45a5 (patch) | |
tree | 00d2bbb524e27727dc111994082f5293e6d12b11 /cmd/commands/commandargs.go | |
parent | 03b6d1f652407fa9c3ec7e740e120a1c3e920de0 (diff) | |
download | chat-b2dd00dd5b83fc7e8b311a55f5a2536e4f3d45a5.tar.gz chat-b2dd00dd5b83fc7e8b311a55f5a2536e4f3d45a5.tar.bz2 chat-b2dd00dd5b83fc7e8b311a55f5a2536e4f3d45a5.zip |
Adding enterprise commands support (#8327)
Diffstat (limited to 'cmd/commands/commandargs.go')
-rw-r--r-- | cmd/commands/commandargs.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/cmd/commands/commandargs.go b/cmd/commands/commandargs.go new file mode 100644 index 000000000..702f01c9a --- /dev/null +++ b/cmd/commands/commandargs.go @@ -0,0 +1,64 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package commands + +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 +} |