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