Commit 4642a6ae authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: uuid function

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