2020-12-11 20:23:08 +00:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
UUID "github.com/hashicorp/go-uuid"
|
|
|
|
)
|
|
|
|
|
2021-01-13 17:49:17 +00:00
|
|
|
func unixTime() string {
|
2020-12-11 20:23:08 +00:00
|
|
|
return strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
|
}
|
|
|
|
|
2021-01-13 17:49:17 +00:00
|
|
|
func unixTimeMillis() string {
|
|
|
|
return strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
func timestamp(format string) string {
|
|
|
|
return time.Now().Format(format)
|
2020-12-11 20:23:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func truncate(maxLen int, str string) (string, error) {
|
|
|
|
if maxLen <= 0 {
|
|
|
|
return "", fmt.Errorf("max length must be > 0 but was %d", maxLen)
|
|
|
|
}
|
|
|
|
if len(str) > maxLen {
|
|
|
|
return str[:maxLen], nil
|
|
|
|
}
|
|
|
|
return str, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
sha256HashLen = 8
|
|
|
|
)
|
|
|
|
|
|
|
|
func truncateSHA256(maxLen int, str string) (string, error) {
|
|
|
|
if maxLen <= 8 {
|
|
|
|
return "", fmt.Errorf("max length must be > 8 but was %d", maxLen)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(str) <= maxLen {
|
|
|
|
return str, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
truncIndex := maxLen - sha256HashLen
|
|
|
|
hash := hashSHA256(str[truncIndex:])
|
|
|
|
result := fmt.Sprintf("%s%s", str[:truncIndex], hash[:sha256HashLen])
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashSHA256(str string) string {
|
|
|
|
return fmt.Sprintf("%x", sha256.Sum256([]byte(str)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func uppercase(str string) string {
|
|
|
|
return strings.ToUpper(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func lowercase(str string) string {
|
|
|
|
return strings.ToLower(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func replace(find string, replace string, str string) string {
|
|
|
|
return strings.ReplaceAll(str, find, replace)
|
|
|
|
}
|
|
|
|
|
|
|
|
func uuid() (string, error) {
|
|
|
|
return UUID.GenerateUUID()
|
|
|
|
}
|