packer: uuid function

This commit is contained in:
Mitchell Hashimoto 2013-09-05 12:19:56 -07:00
parent e89f182b33
commit 8dd3850482
2 changed files with 23 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package packer
import ( import (
"bytes" "bytes"
"cgl.tideland.biz/identifier"
"encoding/hex"
"fmt" "fmt"
"strconv" "strconv"
"text/template" "text/template"
@ -28,6 +30,7 @@ func NewConfigTemplate() (*ConfigTemplate, error) {
result.root.Funcs(template.FuncMap{ result.root.Funcs(template.FuncMap{
"timestamp": templateTimestamp, "timestamp": templateTimestamp,
"user": result.templateUser, "user": result.templateUser,
"uuid": templateUuid,
}) })
return result, nil return result, nil
@ -79,3 +82,7 @@ func (t *ConfigTemplate) templateUser(n string) (string, error) {
func templateTimestamp() string { func templateTimestamp() string {
return strconv.FormatInt(time.Now().UTC().Unix(), 10) return strconv.FormatInt(time.Now().UTC().Unix(), 10)
} }
func templateUuid() string {
return hex.EncodeToString(identifier.NewUUID().Raw())
}

View File

@ -47,6 +47,22 @@ func TestConfigTemplateProcess_user(t *testing.T) {
} }
} }
func TestConfigTemplateProcess_uuid(t *testing.T) {
tpl, err := NewConfigTemplate()
if err != nil {
t.Fatalf("err: %s", err)
}
result, err := tpl.Process(`{{uuid}}`, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if len(result) != 32 {
t.Fatalf("err: %s", result)
}
}
func TestConfigTemplateValidate(t *testing.T) { func TestConfigTemplateValidate(t *testing.T) {
tpl, err := NewConfigTemplate() tpl, err := NewConfigTemplate()
if err != nil { if err != nil {