packer: uuid function
This commit is contained in:
parent
687352fd5a
commit
4642a6aee1
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue