forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
174 lines (152 loc) · 4.09 KB
/
template.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package template
import (
"crypto/md5"
"errors"
"fmt"
"html/template"
"io"
"strings"
"github.com/GeertJohan/go.rice"
)
// ErrTemplateNotFound indicates the requested template
// does not exists in the TemplateStore.
var ErrTemplateNotFound = errors.New("Template Not Found")
// registry stores a map of Templates where the key
// is the template name and the value is the *template.Template.
var registry = map[string]*template.Template{}
// ExecuteTemplate applies the template associated with t that has
// the given name to the specified data object and writes the output to wr.
func ExecuteTemplate(wr io.Writer, name string, data interface{}) error {
templ, ok := registry[name]
if !ok {
return ErrTemplateNotFound
}
return templ.ExecuteTemplate(wr, "_", data)
}
// all template are loaded on initialization.
func init() {
// location of templates
box := rice.MustFindBox("pages")
// these are all the files we need to parse. it is
// kind of annoying that we can't list files in the
// box, and have to enumerate each file here, but it is
// a small price to pay to embed everything and simplify
// the user installation process :)
var files = []string{
// these templates use the form.html
// shared layout
"login.html",
"login_error.html",
"forgot.html",
"forgot_sent.html",
"reset.html",
"signup.html",
"register.html",
"install.html",
// these templates use the default.html
// shared layout
"403.html",
"404.html",
"500.html",
"user_dashboard.html",
"user_password.html",
"user_profile.html",
"user_delete.html",
"user_teams.html",
"user_teams_add.html",
"team_dashboard.html",
"team_profile.html",
"team_members.html",
"team_delete.html",
"members_add.html",
"members_edit.html",
"repo_dashboard.html",
"repo_settings.html",
"repo_delete.html",
"repo_params.html",
"repo_badges.html",
"repo_keys.html",
"repo_commit.html",
"admin_users.html",
"admin_users_edit.html",
"admin_users_add.html",
"admin_settings.html",
"github_add.html",
"github_link.html",
}
// extract the base template as a string
base, err := box.String("base.html")
if err != nil {
panic(err)
}
assets := rice.MustFindBox("../../cmd/droned/assets")
mainjs, err := assets.String("js/main.js")
if err != nil {
panic(err)
}
h := md5.New()
io.WriteString(h, mainjs)
jshash := fmt.Sprintf("%x", h.Sum(nil))
base = strings.Replace(base, "main.js", "main.js?h="+jshash, 1)
// extract the base form template as a string
form, err := box.String("form.html")
if err != nil {
panic(err)
}
// loop through files and create templates
for i, file := range files {
// extract template from box
page, err := box.String(file)
if err != nil {
panic(err)
}
// HACK: choose which base template to use FOR THE RECORD I
// don't really like this, but it works for now.
var baseTemplate = base
if i < 8 {
baseTemplate = form
}
// parse the template and then add to the global map
baseParsed, err := template.New("_").Parse(baseTemplate)
if err != nil {
panic(fmt.Errorf("Error parsing base.html template: %s", err))
}
pageParsed, err := baseParsed.Parse(page)
if err != nil {
panic(fmt.Errorf("Error parsing page template for %s: %s", file, err))
}
registry[file] = pageParsed
}
// location of templates
box = rice.MustFindBox("emails")
files = []string{
"activation.html",
"failure.html",
"success.html",
"invitation.html",
"reset_password.html",
}
// extract the base template as a string
base, err = box.String("base_email.html")
if err != nil {
panic(err)
}
// loop through files and create templates
for _, file := range files {
// extract template from box
email, err := box.String(file)
if err != nil {
panic(err)
}
baseParsed, err := template.New("_").Parse(base)
if err != nil {
panic(fmt.Errorf("Error parsing base_email.html template: %s", err))
}
emailParsed, err := baseParsed.Parse(email)
if err != nil {
panic(fmt.Errorf("Error parsing email template for %s: %s", file, err))
}
// parse the template and then add to the global map
registry[file] = emailParsed
}
}