github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/app/slashcommands/util.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package slashcommands
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/masterhung0112/hk_server/v5/model"
    12  	"github.com/masterhung0112/hk_server/v5/shared/i18n"
    13  )
    14  
    15  const (
    16  	ActionKey = "-action"
    17  )
    18  
    19  // responsef creates an ephemeral command response using printf syntax.
    20  func responsef(format string, args ...interface{}) *model.CommandResponse {
    21  	return &model.CommandResponse{
    22  		ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
    23  		Text:         fmt.Sprintf(format, args...),
    24  		Type:         model.POST_DEFAULT,
    25  	}
    26  }
    27  
    28  // parseNamedArgs parses a command string into a map of arguments. It is assumed the
    29  // command string is of the form `<action> --arg1 value1 ...` Supports empty values.
    30  // Arg names are limited to [0-9a-zA-Z_].
    31  func parseNamedArgs(cmd string) map[string]string {
    32  	m := make(map[string]string)
    33  
    34  	split := strings.Fields(cmd)
    35  
    36  	// check for optional action
    37  	if len(split) >= 2 && !strings.HasPrefix(split[1], "--") {
    38  		m[ActionKey] = split[1] // prefix with hyphen to avoid collision with arg named "action"
    39  	}
    40  
    41  	for i := 0; i < len(split); i++ {
    42  		if !strings.HasPrefix(split[i], "--") {
    43  			continue
    44  		}
    45  		var val string
    46  		arg := trimSpaceAndQuotes(strings.Trim(split[i], "-"))
    47  		if i < len(split)-1 && !strings.HasPrefix(split[i+1], "--") {
    48  			val = trimSpaceAndQuotes(split[i+1])
    49  		}
    50  		if arg != "" {
    51  			m[arg] = val
    52  		}
    53  	}
    54  	return m
    55  }
    56  
    57  func trimSpaceAndQuotes(s string) string {
    58  	trimmed := strings.TrimSpace(s)
    59  	trimmed = strings.TrimPrefix(trimmed, "\"")
    60  	trimmed = strings.TrimPrefix(trimmed, "'")
    61  	trimmed = strings.TrimSuffix(trimmed, "\"")
    62  	trimmed = strings.TrimSuffix(trimmed, "'")
    63  	return trimmed
    64  }
    65  
    66  func parseBool(s string) (bool, error) {
    67  	switch strings.ToLower(s) {
    68  	case "1", "t", "true", "yes", "y":
    69  		return true, nil
    70  	case "0", "f", "false", "no", "n":
    71  		return false, nil
    72  	}
    73  	return false, fmt.Errorf("cannot parse '%s' as a boolean", s)
    74  }
    75  
    76  func formatBool(fn i18n.TranslateFunc, b bool) string {
    77  	if b {
    78  		return fn("True")
    79  	}
    80  	return fn("False")
    81  }
    82  
    83  func formatTimestamp(timestamp int64) string {
    84  	if timestamp == 0 {
    85  		return "--"
    86  	}
    87  
    88  	ts := model.GetTimeForMillis(timestamp)
    89  
    90  	if !isToday(ts) {
    91  		return ts.Format("Jan 2 15:04:05 MST 2006")
    92  	}
    93  	date := ts.Format("15:04:05 MST 2006")
    94  	return fmt.Sprintf("Today %s", date)
    95  }
    96  
    97  func isToday(ts time.Time) bool {
    98  	now := time.Now()
    99  	year, month, day := ts.Date()
   100  	nowYear, nowMonth, nowDay := now.Date()
   101  	return year == nowYear && month == nowMonth && day == nowDay
   102  }