2016-12-15 12:03:02 +01:00
|
|
|
package sprig
|
|
|
|
|
|
|
|
import (
|
2017-05-18 18:54:23 +02:00
|
|
|
"errors"
|
2016-12-15 12:03:02 +01:00
|
|
|
"html/template"
|
|
|
|
"os"
|
2016-12-21 10:25:12 +01:00
|
|
|
"path"
|
2016-12-15 12:03:02 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
ttemplate "text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
util "github.com/aokoli/goutils"
|
2017-05-18 18:54:23 +02:00
|
|
|
"github.com/huandu/xstrings"
|
2016-12-15 12:03:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Produce the function map.
|
|
|
|
//
|
|
|
|
// Use this to pass the functions into the template engine:
|
|
|
|
//
|
2017-03-31 18:01:58 +02:00
|
|
|
// tpl := template.New("foo").Funcs(sprig.FuncMap()))
|
2016-12-15 12:03:02 +01:00
|
|
|
//
|
|
|
|
func FuncMap() template.FuncMap {
|
|
|
|
return HtmlFuncMap()
|
|
|
|
}
|
|
|
|
|
|
|
|
// HermeticTextFuncMap returns a 'text/template'.FuncMap with only repeatable functions.
|
|
|
|
func HermeticTxtFuncMap() ttemplate.FuncMap {
|
|
|
|
r := TxtFuncMap()
|
|
|
|
for _, name := range nonhermeticFunctions {
|
|
|
|
delete(r, name)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// HermeticHtmlFuncMap returns an 'html/template'.Funcmap with only repeatable functions.
|
|
|
|
func HermeticHtmlFuncMap() template.FuncMap {
|
|
|
|
r := HtmlFuncMap()
|
|
|
|
for _, name := range nonhermeticFunctions {
|
|
|
|
delete(r, name)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// TextFuncMap returns a 'text/template'.FuncMap
|
|
|
|
func TxtFuncMap() ttemplate.FuncMap {
|
2017-03-31 18:01:58 +02:00
|
|
|
return ttemplate.FuncMap(GenericFuncMap())
|
2016-12-15 12:03:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// HtmlFuncMap returns an 'html/template'.Funcmap
|
|
|
|
func HtmlFuncMap() template.FuncMap {
|
2017-03-31 18:01:58 +02:00
|
|
|
return template.FuncMap(GenericFuncMap())
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenericFuncMap returns a copy of the basic function map as a map[string]interface{}.
|
|
|
|
func GenericFuncMap() map[string]interface{} {
|
|
|
|
gfm := make(map[string]interface{}, len(genericMap))
|
|
|
|
for k, v := range genericMap {
|
|
|
|
gfm[k] = v
|
|
|
|
}
|
|
|
|
return gfm
|
2016-12-15 12:03:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// These functions are not guaranteed to evaluate to the same result for given input, because they
|
|
|
|
// refer to the environemnt or global state.
|
|
|
|
var nonhermeticFunctions = []string{
|
|
|
|
// Date functions
|
|
|
|
"date",
|
|
|
|
"date_in_zone",
|
|
|
|
"date_modify",
|
|
|
|
"now",
|
|
|
|
"htmlDate",
|
|
|
|
"htmlDateInZone",
|
|
|
|
"dateInZone",
|
|
|
|
"dateModify",
|
|
|
|
|
|
|
|
// Strings
|
|
|
|
"randAlphaNum",
|
|
|
|
"randAlpha",
|
|
|
|
"randAscii",
|
|
|
|
"randNumeric",
|
|
|
|
"uuidv4",
|
|
|
|
|
|
|
|
// OS
|
|
|
|
"env",
|
|
|
|
"expandenv",
|
|
|
|
}
|
|
|
|
|
|
|
|
var genericMap = map[string]interface{}{
|
|
|
|
"hello": func() string { return "Hello!" },
|
|
|
|
|
|
|
|
// Date functions
|
|
|
|
"date": date,
|
|
|
|
"date_in_zone": dateInZone,
|
|
|
|
"date_modify": dateModify,
|
|
|
|
"now": func() time.Time { return time.Now() },
|
|
|
|
"htmlDate": htmlDate,
|
|
|
|
"htmlDateInZone": htmlDateInZone,
|
|
|
|
"dateInZone": dateInZone,
|
|
|
|
"dateModify": dateModify,
|
2017-12-19 14:00:29 +01:00
|
|
|
"ago": dateAgo,
|
|
|
|
"toDate": toDate,
|
2016-12-15 12:03:02 +01:00
|
|
|
|
|
|
|
// Strings
|
|
|
|
"abbrev": abbrev,
|
|
|
|
"abbrevboth": abbrevboth,
|
|
|
|
"trunc": trunc,
|
|
|
|
"trim": strings.TrimSpace,
|
|
|
|
"upper": strings.ToUpper,
|
|
|
|
"lower": strings.ToLower,
|
|
|
|
"title": strings.Title,
|
|
|
|
"untitle": untitle,
|
|
|
|
"substr": substring,
|
|
|
|
// Switch order so that "foo" | repeat 5
|
|
|
|
"repeat": func(count int, str string) string { return strings.Repeat(str, count) },
|
|
|
|
// Deprecated: Use trimAll.
|
|
|
|
"trimall": func(a, b string) string { return strings.Trim(b, a) },
|
|
|
|
// Switch order so that "$foo" | trimall "$"
|
|
|
|
"trimAll": func(a, b string) string { return strings.Trim(b, a) },
|
|
|
|
"trimSuffix": func(a, b string) string { return strings.TrimSuffix(b, a) },
|
|
|
|
"trimPrefix": func(a, b string) string { return strings.TrimPrefix(b, a) },
|
|
|
|
"nospace": util.DeleteWhiteSpace,
|
|
|
|
"initials": initials,
|
|
|
|
"randAlphaNum": randAlphaNumeric,
|
|
|
|
"randAlpha": randAlpha,
|
|
|
|
"randAscii": randAscii,
|
|
|
|
"randNumeric": randNumeric,
|
|
|
|
"swapcase": util.SwapCase,
|
2017-05-18 18:54:23 +02:00
|
|
|
"shuffle": xstrings.Shuffle,
|
|
|
|
"snakecase": xstrings.ToSnakeCase,
|
|
|
|
"camelcase": xstrings.ToCamelCase,
|
2016-12-15 12:03:02 +01:00
|
|
|
"wrap": func(l int, s string) string { return util.Wrap(s, l) },
|
|
|
|
"wrapWith": func(l int, sep, str string) string { return util.WrapCustom(str, l, sep, true) },
|
|
|
|
// Switch order so that "foobar" | contains "foo"
|
|
|
|
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },
|
|
|
|
"hasPrefix": func(substr string, str string) bool { return strings.HasPrefix(str, substr) },
|
|
|
|
"hasSuffix": func(substr string, str string) bool { return strings.HasSuffix(str, substr) },
|
|
|
|
"quote": quote,
|
|
|
|
"squote": squote,
|
|
|
|
"cat": cat,
|
|
|
|
"indent": indent,
|
2017-12-19 14:00:29 +01:00
|
|
|
"nindent": nindent,
|
2016-12-15 12:03:02 +01:00
|
|
|
"replace": replace,
|
|
|
|
"plural": plural,
|
|
|
|
"sha256sum": sha256sum,
|
2017-03-31 18:01:58 +02:00
|
|
|
"toString": strval,
|
2016-12-15 12:03:02 +01:00
|
|
|
|
|
|
|
// Wrap Atoi to stop errors.
|
|
|
|
"atoi": func(a string) int { i, _ := strconv.Atoi(a); return i },
|
|
|
|
"int64": toInt64,
|
|
|
|
"int": toInt,
|
|
|
|
"float64": toFloat64,
|
|
|
|
|
|
|
|
//"gt": func(a, b int) bool {return a > b},
|
|
|
|
//"gte": func(a, b int) bool {return a >= b},
|
|
|
|
//"lt": func(a, b int) bool {return a < b},
|
|
|
|
//"lte": func(a, b int) bool {return a <= b},
|
|
|
|
|
|
|
|
// split "/" foo/bar returns map[int]string{0: foo, 1: bar}
|
2017-03-31 18:01:58 +02:00
|
|
|
"split": split,
|
|
|
|
"splitList": func(sep, orig string) []string { return strings.Split(orig, sep) },
|
|
|
|
"toStrings": strslice,
|
2016-12-15 12:03:02 +01:00
|
|
|
|
|
|
|
"until": until,
|
|
|
|
"untilStep": untilStep,
|
|
|
|
|
|
|
|
// VERY basic arithmetic.
|
|
|
|
"add1": func(i interface{}) int64 { return toInt64(i) + 1 },
|
|
|
|
"add": func(i ...interface{}) int64 {
|
|
|
|
var a int64 = 0
|
|
|
|
for _, b := range i {
|
|
|
|
a += toInt64(b)
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
},
|
|
|
|
"sub": func(a, b interface{}) int64 { return toInt64(a) - toInt64(b) },
|
|
|
|
"div": func(a, b interface{}) int64 { return toInt64(a) / toInt64(b) },
|
|
|
|
"mod": func(a, b interface{}) int64 { return toInt64(a) % toInt64(b) },
|
|
|
|
"mul": func(a interface{}, v ...interface{}) int64 {
|
|
|
|
val := toInt64(a)
|
|
|
|
for _, b := range v {
|
|
|
|
val = val * toInt64(b)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
},
|
|
|
|
"biggest": max,
|
|
|
|
"max": max,
|
|
|
|
"min": min,
|
2017-12-19 14:00:29 +01:00
|
|
|
"ceil": ceil,
|
|
|
|
"floor": floor,
|
|
|
|
"round": round,
|
2016-12-15 12:03:02 +01:00
|
|
|
|
|
|
|
// string slices. Note that we reverse the order b/c that's better
|
|
|
|
// for template processing.
|
2017-03-31 18:01:58 +02:00
|
|
|
"join": join,
|
|
|
|
"sortAlpha": sortAlpha,
|
2016-12-15 12:03:02 +01:00
|
|
|
|
|
|
|
// Defaults
|
2017-05-18 18:54:23 +02:00
|
|
|
"default": dfault,
|
|
|
|
"empty": empty,
|
|
|
|
"coalesce": coalesce,
|
|
|
|
"compact": compact,
|
|
|
|
"toJson": toJson,
|
|
|
|
"toPrettyJson": toPrettyJson,
|
2016-12-15 12:03:02 +01:00
|
|
|
|
|
|
|
// Reflection
|
|
|
|
"typeOf": typeOf,
|
|
|
|
"typeIs": typeIs,
|
|
|
|
"typeIsLike": typeIsLike,
|
|
|
|
"kindOf": kindOf,
|
|
|
|
"kindIs": kindIs,
|
|
|
|
|
|
|
|
// OS:
|
|
|
|
"env": func(s string) string { return os.Getenv(s) },
|
|
|
|
"expandenv": func(s string) string { return os.ExpandEnv(s) },
|
|
|
|
|
2016-12-21 10:25:12 +01:00
|
|
|
// File Paths:
|
|
|
|
"base": path.Base,
|
|
|
|
"dir": path.Dir,
|
|
|
|
"clean": path.Clean,
|
|
|
|
"ext": path.Ext,
|
|
|
|
"isAbs": path.IsAbs,
|
|
|
|
|
2016-12-15 12:03:02 +01:00
|
|
|
// Encoding:
|
|
|
|
"b64enc": base64encode,
|
|
|
|
"b64dec": base64decode,
|
|
|
|
"b32enc": base32encode,
|
|
|
|
"b32dec": base32decode,
|
|
|
|
|
|
|
|
// Data Structures:
|
2017-03-31 18:01:58 +02:00
|
|
|
"tuple": list, // FIXME: with the addition of append/prepend these are no longer immutable.
|
|
|
|
"list": list,
|
2016-12-21 10:25:12 +01:00
|
|
|
"dict": dict,
|
|
|
|
"set": set,
|
|
|
|
"unset": unset,
|
|
|
|
"hasKey": hasKey,
|
2017-03-31 18:01:58 +02:00
|
|
|
"pluck": pluck,
|
|
|
|
"keys": keys,
|
|
|
|
"pick": pick,
|
|
|
|
"omit": omit,
|
2017-05-18 18:54:23 +02:00
|
|
|
"merge": merge,
|
2017-03-31 18:01:58 +02:00
|
|
|
|
|
|
|
"append": push, "push": push,
|
|
|
|
"prepend": prepend,
|
|
|
|
"first": first,
|
|
|
|
"rest": rest,
|
|
|
|
"last": last,
|
|
|
|
"initial": initial,
|
|
|
|
"reverse": reverse,
|
|
|
|
"uniq": uniq,
|
|
|
|
"without": without,
|
2017-12-19 14:00:29 +01:00
|
|
|
"has": has,
|
2016-12-15 12:03:02 +01:00
|
|
|
|
|
|
|
// Crypto:
|
2017-12-19 14:00:29 +01:00
|
|
|
"genPrivateKey": generatePrivateKey,
|
|
|
|
"derivePassword": derivePassword,
|
|
|
|
"genCA": generateCertificateAuthority,
|
|
|
|
"genSelfSignedCert": generateSelfSignedCertificate,
|
|
|
|
"genSignedCert": generateSignedCertificate,
|
2016-12-15 12:03:02 +01:00
|
|
|
|
|
|
|
// UUIDs:
|
|
|
|
"uuidv4": uuidv4,
|
|
|
|
|
2017-03-31 18:01:58 +02:00
|
|
|
// SemVer:
|
|
|
|
"semver": semver,
|
|
|
|
"semverCompare": semverCompare,
|
2017-05-18 18:54:23 +02:00
|
|
|
|
|
|
|
// Flow Control:
|
|
|
|
"fail": func(msg string) (string, error) { return "", errors.New(msg) },
|
2017-12-19 14:00:29 +01:00
|
|
|
|
|
|
|
// Regex
|
|
|
|
"regexMatch": regexMatch,
|
|
|
|
"regexFindAll": regexFindAll,
|
|
|
|
"regexFind": regexFind,
|
|
|
|
"regexReplaceAll": regexReplaceAll,
|
|
|
|
"regexReplaceAllLiteral": regexReplaceAllLiteral,
|
|
|
|
"regexSplit": regexSplit,
|
2016-12-15 12:03:02 +01:00
|
|
|
}
|