-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
50 lines (38 loc) · 847 Bytes
/
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 goadmin
import (
"embed"
"io"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
type FSLoader struct {
fs *embed.FS
}
func NewFSLoader(fs *embed.FS) *FSLoader {
return &FSLoader{
fs: fs,
}
}
func (l *FSLoader) Exists(templatePath string) bool {
templatePath = strings.TrimLeft(templatePath, "/")
entries, err := l.fs.ReadDir(filepath.Dir(templatePath))
if err != nil {
return false
}
fileName := filepath.Base(templatePath)
for _, entry := range entries {
if !entry.IsDir() && entry.Name() == fileName {
return true
}
}
return false
}
func (l *FSLoader) Open(templatePath string) (io.ReadCloser, error) {
templatePath = strings.TrimLeft(templatePath, "/")
r, err := l.fs.Open(templatePath)
if err != nil {
return nil, errors.Wrapf(err, "cannot open %q file", templatePath)
}
return r, nil
}