-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add text/template * Simplify templating command * Update command
- Loading branch information
Showing
15 changed files
with
110 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,37 @@ | ||
package core | ||
|
||
import ( | ||
"github.com/flosch/pongo2" | ||
"strings" | ||
"github.com/Masterminds/sprig" | ||
"text/template" | ||
"errors" | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
func RenderTemplate(tmpl string, env map[string]string, args map[string]string) (string, error) { | ||
tpl, err := pongo2.FromString(tmpl) | ||
if err != nil { | ||
return "", err | ||
w := new(bytes.Buffer) | ||
params := union(env, args) | ||
funcs := template.FuncMap{ | ||
"required": templateRequired, | ||
} | ||
|
||
out, err := tpl.Execute(pongo2.Context{"env":env, "args":args}) | ||
tpl, err := template.New("test"). | ||
Funcs(sprig.TxtFuncMap()). | ||
Funcs(funcs). | ||
Option("missingkey=zero"). | ||
Parse(tmpl) | ||
if err != nil { | ||
return "", err | ||
} else { | ||
return out, nil | ||
} | ||
|
||
err = tpl.Execute(w, params) | ||
return w.String(), err | ||
} | ||
|
||
func filterRequired(in *pongo2.Value, param *pongo2.Value) (out *pongo2.Value, err *pongo2.Error) { | ||
if len(strings.TrimSpace(in.String())) == 0 { | ||
return in, &(pongo2.Error{Sender: "filter:required", ErrorMsg: "required parameter missing"}) | ||
func templateRequired(s string) (interface{}, error) { | ||
if len(s) > 0 { | ||
return s, nil | ||
} else { | ||
return in, nil | ||
return s, errors.New(fmt.Sprintf("variable not provided to template")) | ||
} | ||
} | ||
|
||
func init() { | ||
pongo2.RegisterFilter("required", filterRequired) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
I am a template. | ||
PARAM1={{env.PARAM1}} | ||
PARAM2={{env.PARAM2}} | ||
PARAM1={{.PARAM1}} | ||
PARAM2={{.PARAM2}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters