From 19acf16d5d1889b22f76a568f2fa31a397229bf5 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Wed, 14 Sep 2016 13:06:04 +0200 Subject: [PATCH] Add export for template functions --- app.go | 17 ++------------- functions/collect.go | 30 ++++++++++++++++++++++++++ func_env.go => functions/func_env.go | 2 +- func_file.go => functions/func_file.go | 2 +- 4 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 functions/collect.go rename func_env.go => functions/func_env.go (93%) rename func_file.go => functions/func_file.go (95%) diff --git a/app.go b/app.go index 31369ea..fef3e32 100644 --- a/app.go +++ b/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) } diff --git a/functions/collect.go b/functions/collect.go new file mode 100644 index 0000000..9a1f883 --- /dev/null +++ b/functions/collect.go @@ -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 +} diff --git a/func_env.go b/functions/func_env.go similarity index 93% rename from func_env.go rename to functions/func_env.go index ea61b3c..05580f1 100644 --- a/func_env.go +++ b/functions/func_env.go @@ -1,4 +1,4 @@ -package main +package functions import "os" diff --git a/func_file.go b/functions/func_file.go similarity index 95% rename from func_file.go rename to functions/func_file.go index 699baa4..f32d00c 100644 --- a/func_file.go +++ b/functions/func_file.go @@ -1,4 +1,4 @@ -package main +package functions import ( "io/ioutil"