-
Notifications
You must be signed in to change notification settings - Fork 124
/
templatefuncs.go
83 lines (72 loc) · 2.11 KB
/
templatefuncs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package service
import (
"fmt"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/model/user"
"github.com/evergreen-ci/gimlet"
)
const defaultHelpURL = "https://github.com/evergreen-ci/evergreen/wiki/How-To-Read-Evergreen"
// FuncOptions are global variables injected into our templating functions.
type TemplateFunctionOptions struct {
WebHome string
HelpHome string
}
// MakeTemplateFuncs creates and registers all of our built-in template functions.
func MakeTemplateFuncs(fo TemplateFunctionOptions) map[string]interface{} {
r := map[string]interface{}{
// DateFormat returns a time Formatted to the given layout and
// time zone. If the time zone is unset, it defaults to
// `America/New_York`.
"DateFormat": func(when time.Time, layout string, timeZone string) string {
if timeZone == "" {
timeZone = "America/New_York" // I ♥ NY
}
loc, err := time.LoadLocation(timeZone)
if err != nil {
return when.Format(layout)
}
whenTZ := when.In(loc)
return whenTZ.Format(layout)
},
// Static returns a link to a static file.
"Static": func(filetype, filename string) string {
return fmt.Sprintf("/static/%s/%s", filetype, filename)
},
// GetTimezone returns the timezone for a user.
// Defaults to "New_York".
"GetTimezone": func(u gimlet.User) string {
usr, ok := u.(*user.DBUser)
if ok && usr != nil && usr.Settings.Timezone != "" {
return usr.Settings.Timezone
}
return "America/New_York"
},
"GetRegion": func(u gimlet.User) string {
usr, ok := u.(*user.DBUser)
if ok && usr != nil && usr.Settings.Region != "" {
return usr.Settings.Region
}
return evergreen.DefaultEC2Region
},
// Trunc cuts off a string to be n characters long.
"Trunc": func(s string, n int) string {
if n > len(s) {
return s
}
return s[0:n]
},
// HelpUrl returns the address of the Evergreen help page,
// if one is set.
"HelpUrl": func() string {
if fo.HelpHome != "" {
return fo.HelpHome
}
return defaultHelpURL
},
}
r["BuildRevision"] = func() string {
return evergreen.BuildRevision
}
return r
}