Commit f1b3c8a7 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

template/interpolate: build_name and build_type functions

parent 7eff6b11
...@@ -24,6 +24,8 @@ func init() { ...@@ -24,6 +24,8 @@ func init() {
// Funcs are the interpolation funcs that are available within interpolations. // Funcs are the interpolation funcs that are available within interpolations.
var FuncGens = map[string]FuncGenerator{ var FuncGens = map[string]FuncGenerator{
"build_name": funcGenBuildName,
"build_type": funcGenBuildType,
"env": funcGenEnv, "env": funcGenEnv,
"isotime": funcGenIsotime, "isotime": funcGenIsotime,
"pwd": funcGenPwd, "pwd": funcGenPwd,
...@@ -56,6 +58,26 @@ func Funcs(ctx *Context) template.FuncMap { ...@@ -56,6 +58,26 @@ func Funcs(ctx *Context) template.FuncMap {
return template.FuncMap(result) return template.FuncMap(result)
} }
func funcGenBuildName(ctx *Context) interface{} {
return func() (string, error) {
if ctx == nil || ctx.BuildName == "" {
return "", errors.New("build_name not available")
}
return ctx.BuildName, nil
}
}
func funcGenBuildType(ctx *Context) interface{} {
return func() (string, error) {
if ctx == nil || ctx.BuildType == "" {
return "", errors.New("build_name not available")
}
return ctx.BuildType, nil
}
}
func funcGenEnv(ctx *Context) interface{} { func funcGenEnv(ctx *Context) interface{} {
return func(k string) (string, error) { return func(k string) (string, error) {
if !ctx.EnableEnv { if !ctx.EnableEnv {
......
...@@ -8,6 +8,56 @@ import ( ...@@ -8,6 +8,56 @@ import (
"time" "time"
) )
func TestFuncBuildName(t *testing.T) {
cases := []struct {
Input string
Output string
}{
{
`{{build_name}}`,
"foo",
},
}
ctx := &Context{BuildName: "foo"}
for _, tc := range cases {
i := &I{Value: tc.Input}
result, err := i.Render(ctx)
if err != nil {
t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err)
}
if result != tc.Output {
t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result)
}
}
}
func TestFuncBuildType(t *testing.T) {
cases := []struct {
Input string
Output string
}{
{
`{{build_type}}`,
"foo",
},
}
ctx := &Context{BuildType: "foo"}
for _, tc := range cases {
i := &I{Value: tc.Input}
result, err := i.Render(ctx)
if err != nil {
t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err)
}
if result != tc.Output {
t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result)
}
}
}
func TestFuncEnv(t *testing.T) { func TestFuncEnv(t *testing.T) {
cases := []struct { cases := []struct {
Input string Input string
......
...@@ -14,16 +14,23 @@ type Context struct { ...@@ -14,16 +14,23 @@ type Context struct {
// Funcs are extra functions available in the template // Funcs are extra functions available in the template
Funcs map[string]interface{} Funcs map[string]interface{}
// TemplatePath is the path to the template that this is being
// rendered within.
TemplatePath string
// UserVariables is the mapping of user variables that the // UserVariables is the mapping of user variables that the
// "user" function reads from. // "user" function reads from.
UserVariables map[string]string UserVariables map[string]string
// EnableEnv enables the env function // EnableEnv enables the env function
EnableEnv bool EnableEnv bool
// 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
} }
// Render is shorthand for constructing an I and calling Render. // Render is shorthand for constructing an I and calling Render.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment