github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/cmd/mattermost/commands/commandargs.go (about)

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