-
Notifications
You must be signed in to change notification settings - Fork 2
/
template_loader.go
50 lines (42 loc) · 959 Bytes
/
template_loader.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
package errata
import (
"embed"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/flosch/pongo2/v5"
)
var (
//go:embed templates/*
templates embed.FS
embeddedFS = pongo2.NewSet("embedded", pongo2.NewFSLoader(templates))
)
type templateLoader struct {
path string
builtin bool
}
func loaderFromPath(given string) (*templateLoader, error) {
// first check if this is a built-in template (no file extension, language name only)
if filepath.Ext(given) == "" {
file := fmt.Sprintf("%s.tmpl", given)
path := fmt.Sprintf("templates/%s", file)
_, err := templates.Open(path)
if err != nil {
return nil, NewFileNotFoundErr(err, path)
}
return &templateLoader{
path: path,
builtin: true,
}, nil
}
// next try resolve the path literally
_, err := os.Stat(given)
if errors.Is(err, os.ErrNotExist) {
return nil, NewFileNotFoundErr(err, given)
}
return &templateLoader{
path: given,
builtin: false,
}, nil
}