-
Notifications
You must be signed in to change notification settings - Fork 14
/
templates.go
53 lines (47 loc) · 1.19 KB
/
templates.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
package gen
import (
"embed"
"fmt"
"path"
"strings"
)
//go:embed templates
var templates embed.FS
// Template file extension.
const templateext = ".tmpl"
// BuiltinTemplate loads the named template. Returns an error if the template is
// unknown.
func BuiltinTemplate(name string) (string, error) {
path := fmt.Sprintf("templates/%s%s", name, templateext)
b, err := templates.ReadFile(path)
if err != nil {
return "", fmt.Errorf("unknown template %q", name)
}
return string(b), nil
}
// BuiltinTemplateNames returns all builtin template names.
func BuiltinTemplateNames() []string {
entries, err := templates.ReadDir("templates")
if err != nil {
panic("gen: could not read embedded templates")
}
var names []string
for _, entry := range entries {
filename := entry.Name()
if path.Ext(filename) != templateext {
panic("gen: builtin template has wrong extension")
}
name := strings.TrimSuffix(filename, templateext)
names = append(names, name)
}
return names
}
// IsBuiltinTemplate reports whether name is a builtin template name.
func IsBuiltinTemplate(name string) bool {
for _, builtin := range BuiltinTemplateNames() {
if builtin == name {
return true
}
}
return false
}