2023-11-12 23:54:52 -05:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-11-16 23:21:23 -05:00
|
|
|
"regexp"
|
2023-11-12 23:54:52 -05:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
var tplFuncs = template.FuncMap{
|
2023-11-13 00:44:09 -05:00
|
|
|
"map": tplMap,
|
2023-11-13 13:20:43 -05:00
|
|
|
"cat": tplCat,
|
2023-11-13 18:37:37 -05:00
|
|
|
"split": tplSplit,
|
2023-11-13 00:44:09 -05:00
|
|
|
"join": tplJoin,
|
|
|
|
"under": tplUnder,
|
|
|
|
"varPrefix": tplVarPrefix,
|
2023-11-13 13:42:06 -05:00
|
|
|
"repeat": tplRepeat,
|
|
|
|
"indent": tplIndent,
|
|
|
|
"add": tplAdd,
|
2023-11-13 13:20:43 -05:00
|
|
|
"inc": tplInc,
|
2023-11-13 13:42:06 -05:00
|
|
|
"sub": tplSub,
|
|
|
|
"dec": tplDec,
|
|
|
|
"mult": tplMult,
|
2023-11-13 18:07:24 -05:00
|
|
|
"camel": tplCamel,
|
2023-11-12 23:54:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func tplMap(vals ...any) (map[string]any, error) {
|
|
|
|
if len(vals)%2 != 0 {
|
|
|
|
return nil, fmt.Errorf("missing value, need one key and one value per kv pair")
|
|
|
|
}
|
|
|
|
|
|
|
|
m := make(map[string]any)
|
|
|
|
for i := 0; i < len(vals); i += 2 {
|
|
|
|
m[vals[i].(string)] = vals[i+1]
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2023-11-13 13:20:43 -05:00
|
|
|
func tplCat(strs ...string) string {
|
2023-11-12 23:54:52 -05:00
|
|
|
return strings.Join(strs, "")
|
|
|
|
}
|
2023-11-13 00:44:09 -05:00
|
|
|
|
2023-11-13 18:37:37 -05:00
|
|
|
func tplSplit(s string, sep string) []string {
|
|
|
|
return strings.Split(s, sep)
|
|
|
|
}
|
|
|
|
|
2023-11-13 13:20:43 -05:00
|
|
|
func tplJoin(strs []string, sep string) string {
|
|
|
|
return strings.Join(strs, sep)
|
|
|
|
}
|
|
|
|
|
2023-11-13 00:44:09 -05:00
|
|
|
func tplUnder(s string) string {
|
2023-11-16 23:21:23 -05:00
|
|
|
// Remove all characters that are not alphanumeric or spaces or underscores
|
|
|
|
s = regexp.MustCompile("[^a-zA-Z0-9_ -]+").ReplaceAllString(s, "")
|
|
|
|
|
|
|
|
// Replace all spaces with underscores
|
|
|
|
s = strings.ReplaceAll(s, " ", "_")
|
|
|
|
|
|
|
|
// Replace all dashes with underscores
|
|
|
|
s = strings.ReplaceAll(s, "-", "_")
|
|
|
|
|
|
|
|
// Convert to lowercase
|
|
|
|
s = strings.ToLower(s)
|
|
|
|
|
|
|
|
return s
|
2023-11-13 00:44:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func tplVarPrefix(s string) string {
|
|
|
|
if s == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return tplUnder(s) + "_"
|
|
|
|
}
|
2023-11-13 13:20:43 -05:00
|
|
|
|
2023-11-13 13:42:06 -05:00
|
|
|
func tplRepeat(s string, n int) string {
|
|
|
|
return strings.Repeat(s, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tplIndent(n int) string {
|
|
|
|
return tplRepeat(" ", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tplAdd(a, b int) int {
|
|
|
|
return a + b
|
|
|
|
}
|
|
|
|
|
2023-11-13 13:20:43 -05:00
|
|
|
func tplInc(i int) int {
|
2023-11-13 13:42:06 -05:00
|
|
|
return tplAdd(i, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tplSub(a, b int) int {
|
|
|
|
return a - b
|
|
|
|
}
|
|
|
|
|
|
|
|
func tplDec(i int) int {
|
|
|
|
return tplSub(i, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tplMult(a, b int) int {
|
|
|
|
return a * b
|
2023-11-13 13:20:43 -05:00
|
|
|
}
|
2023-11-13 18:07:24 -05:00
|
|
|
|
|
|
|
func tplCamel(s string) string {
|
2023-11-16 23:21:23 -05:00
|
|
|
// Remove all characters that are not alphanumeric or spaces or underscores
|
|
|
|
s = regexp.MustCompile("[^a-zA-Z0-9_ -]+").ReplaceAllString(s, "")
|
|
|
|
|
|
|
|
// Replace all underscores with spaces
|
|
|
|
s = strings.ReplaceAll(s, "_", " ")
|
|
|
|
|
|
|
|
// Replace all dashes with spaces
|
|
|
|
s = strings.ReplaceAll(s, "-", " ")
|
|
|
|
|
|
|
|
// Title case s
|
|
|
|
words := strings.Split(s, " ")
|
|
|
|
for i, word := range words {
|
|
|
|
words[i] = strings.ToTitle(word)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join words
|
|
|
|
s = strings.Join(words, "")
|
|
|
|
|
|
|
|
return s
|
2023-11-13 18:07:24 -05:00
|
|
|
}
|