mirror of
https://github.com/Luzifer/korvike.git
synced 2024-12-20 11:11:19 +00:00
Add export for template functions
This commit is contained in:
parent
8f1a06703f
commit
19acf16d5d
4 changed files with 34 additions and 17 deletions
17
app.go
17
app.go
|
@ -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
30
functions/collect.go
Normal 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
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package functions
|
||||
|
||||
import "os"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package functions
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
Loading…
Reference in a new issue