Adding support for isotime template variable
This commit is contained in:
parent
1df0735713
commit
fab369bf15
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue