From fab369bf159e238a5c4dad10942cd72c51772eef Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Thu, 5 Sep 2013 11:09:37 -0700 Subject: [PATCH] Adding support for isotime template variable --- packer/config_template.go | 5 +++++ packer/config_template_test.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packer/config_template.go b/packer/config_template.go index 354208cbc..b829521ae 100644 --- a/packer/config_template.go +++ b/packer/config_template.go @@ -26,6 +26,7 @@ func NewConfigTemplate() (*ConfigTemplate, error) { result.root = template.New("configTemplateRoot") result.root.Funcs(template.FuncMap{ + "isotime": templateISOTime, "timestamp": templateTimestamp, "user": result.templateUser, }) @@ -79,3 +80,7 @@ func (t *ConfigTemplate) templateUser(n string) (string, error) { func templateTimestamp() string { return strconv.FormatInt(time.Now().UTC().Unix(), 10) } + +func templateISOTime() string { + return time.Now().UTC().Format(time.RFC3339) +} diff --git a/packer/config_template_test.go b/packer/config_template_test.go index 4c4864cda..646565418 100644 --- a/packer/config_template_test.go +++ b/packer/config_template_test.go @@ -7,6 +7,28 @@ import ( "time" ) +func TestConfigTemplateProcess_isotime(t *testing.T) { + tpl, err := NewConfigTemplate() + if err != nil { + t.Fatalf("err: %s", err) + } + + result, err := tpl.Process(`{{isotime}}`, nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + val, err := time.Parse(time.RFC3339, result) + if err != nil { + t.Fatalf("err: %s", err) + } + + currentTime := time.Now().UTC() + if currentTime.Sub(val) > 2*time.Second { + t.Fatalf("val: %d (current: %d)", val, currentTime) + } +} + func TestConfigTemplateProcess_timestamp(t *testing.T) { tpl, err := NewConfigTemplate() if err != nil {