Skip to content

Commit

Permalink
feat: add match function for templating
Browse files Browse the repository at this point in the history
  • Loading branch information
kha7iq committed Jul 8, 2021
1 parent f8609dc commit 2f22ba6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/dir/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Render() *cli.Command {

for _, v := range paths {
funcMap := sprig.TxtFuncMap()
t := template.Must(template.New(filepath.Base(v)).Funcs(funcMap).ParseFiles(v))
t := template.Must(template.New(filepath.Base(v)).Funcs(funcMap).Funcs(helpers.MatchFunc()).ParseFiles(v))

if len(helpers.GlobalOpts.Prefix) != 0 {
helpers.EnvVariables = helpers.MatchPrefix(helpers.GlobalOpts.Prefix)
Expand Down
10 changes: 9 additions & 1 deletion cmd/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func GetVars() (enVars map[string]string) {
// ParseString will parse any input provided as string
func ParseString(str string) (*template.Template, error) {
funcMap := sprig.TxtFuncMap()
return template.Must(template.New("").Funcs(funcMap).Parse(str)), nil
return template.Must(template.New("").Funcs(funcMap).Funcs(MatchFunc()).Parse(str)), nil
}

// MatchPrefix will match a given prefix pattern of all env variables and render only those.
Expand Down Expand Up @@ -77,3 +77,11 @@ func CreateDirIfNotExist(path string) error {
}
return nil
}

// MatchFunc returns a custom functions map
func MatchFunc() template.FuncMap {
var functionMap = map[string]interface{}{
"match": MatchPrefix,
}
return functionMap
}

0 comments on commit 2f22ba6

Please sign in to comment.