-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_memory_fs.go
58 lines (48 loc) · 1.23 KB
/
in_memory_fs.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
package partial
import (
"io/fs"
"strings"
"time"
)
type InMemoryFS struct {
Files map[string]string
}
func (f *InMemoryFS) AddFile(name, content string) {
if f.Files == nil {
f.Files = make(map[string]string)
}
f.Files[name] = content
}
func (f *InMemoryFS) Open(name string) (fs.File, error) {
content, ok := f.Files[name]
if !ok {
return nil, fs.ErrNotExist
}
return &InMemoryFile{
Reader: strings.NewReader(content),
name: name,
}, nil
}
type InMemoryFile struct {
*strings.Reader
name string
}
func (f *InMemoryFile) Stat() (fs.FileInfo, error) {
return &InMemoryFileInfo{name: f.name, size: int64(f.Len())}, nil
}
func (f *InMemoryFile) ReadDir(count int) ([]fs.DirEntry, error) {
return nil, fs.ErrNotExist
}
func (f *InMemoryFile) Close() error {
return nil
}
type InMemoryFileInfo struct {
name string
size int64
}
func (fi *InMemoryFileInfo) Name() string { return fi.name }
func (fi *InMemoryFileInfo) Size() int64 { return fi.size }
func (fi *InMemoryFileInfo) Mode() fs.FileMode { return 0444 }
func (fi *InMemoryFileInfo) ModTime() time.Time { return time.Time{} }
func (fi *InMemoryFileInfo) IsDir() bool { return false }
func (fi *InMemoryFileInfo) Sys() interface{} { return nil }