-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_system.go
169 lines (145 loc) · 3.51 KB
/
file_system.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
// Copyright 2021 Flamego. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package template
import (
"embed"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
// File is a template file that contains name, data and its extension.
type File interface {
// Name returns the name of the file, stripping its extension. It should return
// "home" not "home.tmpl".
Name() string
// Data returns the file content.
Data() ([]byte, error)
// Ext returns the file extension, carrying the dot ("."). It should return
// ".tmpl" not "tmpl".
Ext() string
}
// FileSystem is a template file system consists a list of template files.
type FileSystem interface {
// Files returns the the list of template files.
Files() []File
}
type file struct {
name string
data []byte
ext string
}
func (f *file) Name() string { return f.name }
func (f *file) Data() ([]byte, error) { return f.data, nil }
func (f *file) Ext() string { return f.ext }
type fileSystem struct {
files []File
}
func (fs *fileSystem) Files() []File { return fs.files }
// isDir returns true if given path is a directory, and returns false when it's
// a file or does not exist.
func isDir(dir string) bool {
f, e := os.Stat(dir)
if e != nil {
return false
}
return f.IsDir()
}
// isFile returns true if given path exists as a file (i.e. not a directory).
func isFile(path string) bool {
f, e := os.Stat(path)
if e != nil {
return false
}
return !f.IsDir()
}
// getExt returns the extension of given name, prefixed with the dot (".").
func getExt(name string) string {
i := strings.Index(name, ".")
if i == -1 {
return ""
}
return name[i:]
}
// newFileSystem constructs and returns a FileSystem from local disk.
func newFileSystem(dir string, allowedExtensions []string) (FileSystem, error) {
var files []File
err := filepath.WalkDir(dir, func(path string, _ fs.DirEntry, err error) error {
if err != nil {
return err
}
ext := getExt(path)
for _, allowed := range allowedExtensions {
if ext != allowed {
continue
}
data, err := os.ReadFile(path)
if err != nil {
return errors.Wrap(err, "read")
}
relpath, err := filepath.Rel(dir, path)
if err != nil {
return errors.Wrap(err, "get relative path")
}
name := filepath.ToSlash(relpath[:len(relpath)-len(ext)])
files = append(files,
&file{
name: name,
data: data,
ext: ext,
},
)
break
}
return nil
})
if err != nil {
return nil, errors.Wrapf(err, "walk %q", dir)
}
return &fileSystem{
files: files,
}, nil
}
// EmbedFS wraps the given embed.FS into a FileSystem.
func EmbedFS(efs embed.FS, dir string, allowedExtensions []string) (FileSystem, error) {
var files []File
err := fs.WalkDir(efs, dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
relpath, err := filepath.Rel(dir, path)
if err != nil {
return errors.Wrap(err, "get relative path")
}
ext := getExt(relpath)
for _, allowed := range allowedExtensions {
if ext != allowed {
continue
}
data, err := efs.ReadFile(path)
if err != nil {
return errors.Wrap(err, "read")
}
name := filepath.ToSlash(relpath[:len(relpath)-len(ext)])
files = append(files,
&file{
name: name,
data: data,
ext: ext,
},
)
}
return nil
})
if err != nil {
return nil, errors.Wrap(err, "walk")
}
return &fileSystem{
files: files,
}, nil
}