packer-cn/template/interpolate/funcs.go

111 lines
2.4 KiB
Go
Raw Normal View History

package interpolate
import (
"errors"
2015-05-16 00:12:54 -04:00
"fmt"
"os"
2015-05-16 00:14:41 -04:00
"strconv"
2015-05-16 00:16:52 -04:00
"strings"
"text/template"
2015-05-16 00:12:54 -04:00
"time"
2015-05-16 00:16:52 -04:00
"github.com/mitchellh/packer/common/uuid"
)
2015-05-16 00:14:41 -04:00
// InitTime is the UTC time when this package was initialized. It is
// used as the timestamp for all configuration templates so that they
// match for a single build.
var InitTime time.Time
func init() {
InitTime = time.Now().UTC()
}
// Funcs are the interpolation funcs that are available within interpolations.
var FuncGens = map[string]FuncGenerator{
2015-05-16 00:14:41 -04:00
"env": funcGenEnv,
"isotime": funcGenIsotime,
"pwd": funcGenPwd,
"timestamp": funcGenTimestamp,
2015-05-16 00:16:52 -04:00
"uuid": funcGenUuid,
2015-05-16 00:14:41 -04:00
"user": funcGenUser,
2015-05-16 00:16:52 -04:00
"upper": funcGenPrimitive(strings.ToUpper),
"lower": funcGenPrimitive(strings.ToLower),
}
// FuncGenerator is a function that given a context generates a template
// function for the template.
type FuncGenerator func(*Context) interface{}
// Funcs returns the functions that can be used for interpolation given
// a context.
func Funcs(ctx *Context) template.FuncMap {
result := make(map[string]interface{})
for k, v := range FuncGens {
result[k] = v(ctx)
}
return template.FuncMap(result)
}
func funcGenEnv(ctx *Context) interface{} {
return func(k string) (string, error) {
if ctx.DisableEnv {
// The error message doesn't have to be that detailed since
// semantic checks should catch this.
return "", errors.New("env vars are not allowed here")
}
return os.Getenv(k), nil
}
}
2015-05-16 00:12:54 -04:00
func funcGenIsotime(ctx *Context) interface{} {
return func(format ...string) (string, error) {
if len(format) == 0 {
return time.Now().UTC().Format(time.RFC3339), nil
}
if len(format) > 1 {
return "", fmt.Errorf("too many values, 1 needed: %v", format)
}
return time.Now().UTC().Format(format[0]), nil
}
}
2015-05-16 00:16:52 -04:00
func funcGenPrimitive(value interface{}) FuncGenerator {
return func(ctx *Context) interface{} {
return value
}
}
2015-05-16 00:10:12 -04:00
func funcGenPwd(ctx *Context) interface{} {
return func() (string, error) {
return os.Getwd()
}
}
2015-05-16 00:14:41 -04:00
func funcGenTimestamp(ctx *Context) interface{} {
return func() string {
return strconv.FormatInt(InitTime.Unix(), 10)
}
}
func funcGenUser(ctx *Context) interface{} {
2015-05-16 00:18:27 -04:00
return func(k string) string {
if ctx == nil || ctx.UserVariables == nil {
return ""
}
return ctx.UserVariables[k]
}
}
2015-05-16 00:16:52 -04:00
func funcGenUuid(ctx *Context) interface{} {
return func() string {
return uuid.TimeOrderedUUID()
}
}