Adding support for isotime template variable

This commit is contained in:
Armon Dadgar 2013-09-05 11:09:37 -07:00
parent 1df0735713
commit fab369bf15
2 changed files with 27 additions and 0 deletions

View File

@ -26,6 +26,7 @@ func NewConfigTemplate() (*ConfigTemplate, error) {
result.root = template.New("configTemplateRoot") result.root = template.New("configTemplateRoot")
result.root.Funcs(template.FuncMap{ result.root.Funcs(template.FuncMap{
"isotime": templateISOTime,
"timestamp": templateTimestamp, "timestamp": templateTimestamp,
"user": result.templateUser, "user": result.templateUser,
}) })
@ -79,3 +80,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 templateISOTime() string {
return time.Now().UTC().Format(time.RFC3339)
}

View File

@ -7,6 +7,28 @@ import (
"time" "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) { func TestConfigTemplateProcess_timestamp(t *testing.T) {
tpl, err := NewConfigTemplate() tpl, err := NewConfigTemplate()
if err != nil { if err != nil {