forked from qor/assetfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.go
88 lines (80 loc) · 1.95 KB
/
filesystem.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
package assetfs
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// AssetFileSystem AssetFS based on FileSystem
type AssetFileSystem struct {
paths []string
nameSpacedFS map[string]Interface
}
// RegisterPath register view paths
func (fs *AssetFileSystem) RegisterPath(pth string) error {
if _, err := os.Stat(pth); !os.IsNotExist(err) {
var existing bool
for _, p := range fs.paths {
if p == pth {
existing = true
break
}
}
if !existing {
fs.paths = append(fs.paths, pth)
}
return nil
}
return errors.New("not found")
}
// PrependPath prepend path to view paths
func (fs *AssetFileSystem) PrependPath(pth string) error {
if _, err := os.Stat(pth); !os.IsNotExist(err) {
var existing bool
for _, p := range fs.paths {
if p == pth {
existing = true
break
}
}
if !existing {
fs.paths = append([]string{pth}, fs.paths...)
}
return nil
}
return errors.New("not found")
}
// Asset get content with name from assetfs
func (fs *AssetFileSystem) Asset(name string) ([]byte, error) {
for _, pth := range fs.paths {
if _, err := os.Stat(filepath.Join(pth, name)); err == nil {
return ioutil.ReadFile(filepath.Join(pth, name))
}
}
return []byte{}, fmt.Errorf("%v not found", name)
}
// Glob list matched files from assetfs
func (fs *AssetFileSystem) Glob(pattern string) (matches []string, err error) {
for _, pth := range fs.paths {
if results, err := filepath.Glob(filepath.Join(pth, pattern)); err == nil {
for _, result := range results {
matches = append(matches, strings.TrimPrefix(result, pth))
}
}
}
return
}
// Compile compile assetfs
func (fs *AssetFileSystem) Compile() error {
return nil
}
// NameSpace return namespaced filesystem
func (fs *AssetFileSystem) NameSpace(nameSpace string) Interface {
if fs.nameSpacedFS == nil {
fs.nameSpacedFS = map[string]Interface{}
}
fs.nameSpacedFS[nameSpace] = &AssetFileSystem{}
return fs.nameSpacedFS[nameSpace]
}