2015-05-16 00:05:47 -04:00
|
|
|
package interpolate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-01-27 13:10:16 -05:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-05-16 00:05:47 -04:00
|
|
|
"text/template"
|
2020-02-14 11:42:29 -05:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2015-05-16 00:05:47 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Context is the context that an interpolation is done in. This defines
|
|
|
|
// things such as available variables.
|
|
|
|
type Context struct {
|
2015-05-16 00:08:46 -04:00
|
|
|
// Data is the data for the template that is available
|
|
|
|
Data interface{}
|
|
|
|
|
2015-05-27 14:10:09 -04:00
|
|
|
// Funcs are extra functions available in the template
|
|
|
|
Funcs map[string]interface{}
|
|
|
|
|
2015-05-16 00:18:27 -04:00
|
|
|
// UserVariables is the mapping of user variables that the
|
|
|
|
// "user" function reads from.
|
|
|
|
UserVariables map[string]string
|
|
|
|
|
2018-08-10 17:25:14 -04:00
|
|
|
// SensitiveVariables is a list of variables to sanitize.
|
|
|
|
SensitiveVariables []string
|
|
|
|
|
2015-05-23 19:06:11 -04:00
|
|
|
// EnableEnv enables the env function
|
|
|
|
EnableEnv bool
|
2015-06-13 16:48:35 -04:00
|
|
|
|
|
|
|
// All the fields below are used for built-in functions.
|
|
|
|
//
|
|
|
|
// BuildName and BuildType are the name and type, respectively,
|
|
|
|
// of the builder being used.
|
|
|
|
//
|
|
|
|
// TemplatePath is the path to the template that this is being
|
|
|
|
// rendered within.
|
|
|
|
BuildName string
|
|
|
|
BuildType string
|
|
|
|
TemplatePath string
|
2015-05-16 00:05:47 -04:00
|
|
|
}
|
|
|
|
|
2019-06-14 04:55:10 -04:00
|
|
|
// NewContext returns an initialized empty context.
|
|
|
|
func NewContext() *Context {
|
|
|
|
return &Context{}
|
|
|
|
}
|
|
|
|
|
2020-02-13 11:35:23 -05:00
|
|
|
// RenderOnce is shorthand for constructing an I and calling Render one time.
|
|
|
|
func RenderOnce(v string, ctx *Context) (string, error) {
|
|
|
|
return (&I{Value: v}).Render(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render is shorthand for constructing an I and calling Render until all variables are rendered.
|
2020-01-27 13:10:16 -05:00
|
|
|
func Render(v string, ctx *Context) (rendered string, err error) {
|
|
|
|
// Keep interpolating until all variables are done
|
|
|
|
// Sometimes a variable can been inside another one
|
|
|
|
for {
|
|
|
|
rendered, err = (&I{Value: v}).Render(ctx)
|
|
|
|
if err != nil || rendered == v {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
v = rendered
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render is shorthand for constructing an I and calling Render.
|
|
|
|
// Use regex to filter variables that are not supposed to be interpolated now
|
|
|
|
func RenderRegex(v string, ctx *Context, regex string) (string, error) {
|
|
|
|
re := regexp.MustCompile(regex)
|
|
|
|
matches := re.FindAllStringSubmatch(v, -1)
|
|
|
|
|
|
|
|
// Replace variables to be excluded with a unique UUID
|
|
|
|
excluded := make(map[string]string)
|
|
|
|
for _, value := range matches {
|
|
|
|
id := uuid.New().String()
|
|
|
|
excluded[id] = value[0]
|
|
|
|
v = strings.ReplaceAll(v, value[0], id)
|
|
|
|
}
|
|
|
|
|
|
|
|
rendered, err := (&I{Value: v}).Render(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return rendered, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace back by the UUID the previously excluded values
|
|
|
|
for id, value := range excluded {
|
|
|
|
rendered = strings.ReplaceAll(rendered, id, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rendered, nil
|
2015-05-23 19:12:32 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:28 -04:00
|
|
|
// Validate is shorthand for constructing an I and calling Validate.
|
|
|
|
func Validate(v string, ctx *Context) error {
|
|
|
|
return (&I{Value: v}).Validate(ctx)
|
|
|
|
}
|
|
|
|
|
2015-05-16 00:05:47 -04:00
|
|
|
// I stands for "interpolation" and is the main interpolation struct
|
|
|
|
// in order to render values.
|
|
|
|
type I struct {
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render renders the interpolation with the given context.
|
2019-03-29 11:50:02 -04:00
|
|
|
func (i *I) Render(ictx *Context) (string, error) {
|
|
|
|
tpl, err := i.template(ictx)
|
2015-05-16 00:05:47 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result bytes.Buffer
|
2015-05-16 00:08:46 -04:00
|
|
|
var data interface{}
|
2019-03-29 11:50:02 -04:00
|
|
|
if ictx != nil {
|
|
|
|
data = ictx.Data
|
2015-05-16 00:08:46 -04:00
|
|
|
}
|
2015-05-16 00:05:47 -04:00
|
|
|
if err := tpl.Execute(&result, data); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.String(), nil
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:28 -04:00
|
|
|
// Validate validates that the template is syntactically valid.
|
|
|
|
func (i *I) Validate(ctx *Context) error {
|
|
|
|
_, err := i.template(ctx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-16 00:05:47 -04:00
|
|
|
func (i *I) template(ctx *Context) (*template.Template, error) {
|
|
|
|
return template.New("root").Funcs(Funcs(ctx)).Parse(i.Value)
|
|
|
|
}
|