1
0
Fork 0
mirror of https://github.com/Luzifer/korvike.git synced 2024-11-14 02:02:41 +00:00
korvike/functions/collect.go
Knut Ahlers e8a8b2f83f
Breaking: Add sprig functions, replace some internal ones
Breaking changes:

- Remove function `b64decode`, use `b64dec`
- Remove function `b64encode`, use `b64enc`
- Remove default from `env`, use `env "MYVAR" | default "..."`
- Remove function `hash`, use `sha1sum` / `sha256sum` / `sha512sum`
- Function `now` returns `time.Time`, use `now | date "[format]"`
- Function `split` now has reversed parameters `split <sep> <str>`

Signed-off-by: Knut Ahlers <knut@ahlers.me>
2024-02-28 23:54:18 +01:00

36 lines
861 B
Go

// Package functions contains custom functions specific to korvike and
// returns the sprig functions with added korvike specific functions
package functions
import (
"log"
"sync"
"text/template"
"github.com/Masterminds/sprig/v3"
)
var (
templateFunctions = sprig.FuncMap()
templateFunctionsLock sync.Mutex
)
func registerFunction(name string, f interface{}) {
templateFunctionsLock.Lock()
defer templateFunctionsLock.Unlock()
if _, ok := templateFunctions[name]; ok {
log.Printf("overwriting existing function %q", name)
}
templateFunctions[name] = f
}
// GetFunctionMap exports all functions used in korvike to be used in own projects
// Example:
//
// import korvike "github.com/Luzifer/korvike"
// tpl := template.New("mytemplate").Funcs(korvike.GetFunctionMap())
func GetFunctionMap() template.FuncMap {
return templateFunctions
}