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

packer: uuid function

parent 687352fd
...@@ -2,6 +2,8 @@ package packer ...@@ -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) { ...@@ -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) { ...@@ -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())
}
...@@ -47,6 +47,22 @@ func TestConfigTemplateProcess_user(t *testing.T) { ...@@ -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 {
......
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