github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/cmd/platform/commandargs.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/mattermost/mattermost-server/app"
    10  	"github.com/mattermost/mattermost-server/model"
    11  )
    12  
    13  const COMMAND_ARGS_SEPARATOR = ":"
    14  
    15  func getCommandsFromCommandArgs(a *app.App, commandArgs []string) []*model.Command {
    16  	commands := make([]*model.Command, 0, len(commandArgs))
    17  
    18  	for _, commandArg := range commandArgs {
    19  		command := getCommandFromCommandArg(a, commandArg)
    20  		commands = append(commands, command)
    21  	}
    22  
    23  	return commands
    24  }
    25  
    26  func parseCommandArg(commandArg string) (string, string) {
    27  	result := strings.SplitN(commandArg, COMMAND_ARGS_SEPARATOR, 2)
    28  
    29  	if len(result) == 1 {
    30  		return "", commandArg
    31  	}
    32  
    33  	return result[0], result[1]
    34  }
    35  
    36  func getCommandFromCommandArg(a *app.App, commandArg string) *model.Command {
    37  	teamArg, commandPart := parseCommandArg(commandArg)
    38  	if teamArg == "" && commandPart == "" {
    39  		return nil
    40  	}
    41  
    42  	var command *model.Command
    43  	if teamArg != "" {
    44  		team := getTeamFromTeamArg(a, teamArg)
    45  		if team == nil {
    46  			return nil
    47  		}
    48  
    49  		if result := <-a.Srv.Store.Command().GetByTrigger(team.Id, commandPart); result.Err == nil {
    50  			command = result.Data.(*model.Command)
    51  		} else {
    52  			fmt.Println(result.Err.Error())
    53  		}
    54  	}
    55  
    56  	if command == nil {
    57  		if result := <-a.Srv.Store.Command().Get(commandPart); result.Err == nil {
    58  			command = result.Data.(*model.Command)
    59  		}
    60  	}
    61  
    62  	return command
    63  }