github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/cmd/mattermost/commands/commandargs.go (about)

     1  // Copyright (c) 2017-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/app"
    11  	"github.com/mattermost/mattermost-server/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  
    50  		if result := <-a.Srv.Store.Command().GetByTrigger(team.Id, commandPart); result.Err == nil {
    51  			command = result.Data.(*model.Command)
    52  		} else {
    53  			fmt.Println(result.Err.Error())
    54  		}
    55  	}
    56  
    57  	if command == nil {
    58  		if result := <-a.Srv.Store.Command().Get(commandPart); result.Err == nil {
    59  			command = result.Data.(*model.Command)
    60  		}
    61  	}
    62  
    63  	return command
    64  }