feat: add sprig template functions support (#131)

* cherry-pick from delmendo

* add tests, docs, and lift hermetic restriction

* remove duplicate funcs that are now provided by sprig

* switch func load order to allow built-ins precedence

* Update chglog.go

Co-authored-by: Dirk Elmendorf <dirk@r26d.com>
This commit is contained in:
Trent Albright 2021-03-28 23:18:36 -04:00 committed by GitHub
parent e523fd471a
commit 4f3fdc4dae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 176 additions and 27 deletions

View file

@ -12,6 +12,7 @@ import (
"text/template"
"time"
"github.com/Masterminds/sprig/v3"
"github.com/tsuyoshiwada/go-gitcmd"
)
@ -324,18 +325,6 @@ func (gen *Generator) render(w io.Writer, unreleased *Unreleased, versions []*Ve
"datetime": func(layout string, input time.Time) string {
return input.Format(layout)
},
// check whether substr is within s
"contains": strings.Contains,
// check whether s begins with prefix
"hasPrefix": strings.HasPrefix,
// check whether s ends with suffix
"hasSuffix": strings.HasSuffix,
// replace the first n instances of old with new
"replace": strings.Replace,
// lower case a string
"lower": strings.ToLower,
// upper case a string
"upper": strings.ToUpper,
// upper case the first character of a string
"upperFirst": func(s string) string {
if len(s) > 0 {
@ -355,7 +344,7 @@ func (gen *Generator) render(w io.Writer, unreleased *Unreleased, versions []*Ve
fname := filepath.Base(gen.config.Template)
t := template.Must(template.New(fname).Funcs(fmap).ParseFiles(gen.config.Template))
t := template.Must(template.New(fname).Funcs(sprig.TxtFuncMap()).Funcs(fmap).ParseFiles(gen.config.Template))
return t.Execute(w, &RenderData{
Info: gen.config.Info,