1
0
Fork 0
mirror of https://github.com/Luzifer/korvike.git synced 2024-11-08 15:30:05 +00:00

Add export for template functions

This commit is contained in:
Knut Ahlers 2016-09-14 13:06:04 +02:00
parent 8f1a06703f
commit 19acf16d5d
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
4 changed files with 34 additions and 17 deletions

17
app.go
View file

@ -7,10 +7,10 @@ import (
"io/ioutil"
"log"
"os"
"sync"
"text/template"
"github.com/Luzifer/go_helpers/env"
korvike "github.com/Luzifer/korvike/functions"
"github.com/Luzifer/rconfig"
)
@ -22,22 +22,9 @@ var (
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
}{}
templateFunctions = template.FuncMap{}
templateFunctionsLock sync.Mutex
version = "dev"
)
func registerFunction(name string, f interface{}) error {
templateFunctionsLock.Lock()
defer templateFunctionsLock.Unlock()
if _, ok := templateFunctions[name]; ok {
return errors.New("Duplicate function for name " + name)
}
templateFunctions[name] = f
return nil
}
func init() {
if err := rconfig.Parse(&cfg); err != nil {
log.Fatalf("Unable to parse commandline options: %s", err)
@ -85,7 +72,7 @@ func main() {
log.Fatalf("Unable to read from input: %s", err)
}
tpl, err := template.New("in").Funcs(templateFunctions).Parse(string(rawTpl))
tpl, err := template.New("in").Funcs(korvike.GetFunctionMap()).Parse(string(rawTpl))
if err != nil {
log.Fatalf("Unable to parse template: %s", err)
}

30
functions/collect.go Normal file
View file

@ -0,0 +1,30 @@
package functions
import (
"errors"
"sync"
"text/template"
)
var (
templateFunctions = template.FuncMap{}
templateFunctionsLock sync.Mutex
)
func registerFunction(name string, f interface{}) error {
templateFunctionsLock.Lock()
defer templateFunctionsLock.Unlock()
if _, ok := templateFunctions[name]; ok {
return errors.New("Duplicate function for name " + name)
}
templateFunctions[name] = f
return nil
}
// 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
}

View file

@ -1,4 +1,4 @@
package main
package functions
import "os"

View file

@ -1,4 +1,4 @@
package main
package functions
import (
"io/ioutil"