This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
129 lines (105 loc) · 2.72 KB
/
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
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
package ops
import (
"encoding/json"
"io"
"os"
"path/filepath"
"time"
"github.com/abdfnx/renio/core/options"
"github.com/abdfnx/renio/tools"
quickjs "github.com/abdfnx/qjs"
"github.com/spf13/afero"
)
type FsDriver struct {
Perms *options.Perms
Fs afero.Fs
}
var _ io.Reader = (*os.File)(nil)
func (fs *FsDriver) ReadFile(ctx *quickjs.Context, path quickjs.Value) quickjs.Value {
data, err := afero.ReadFile(fs.Fs, path.String())
tools.Check(err)
return ctx.String(string(data))
}
func (fs *FsDriver) WriteFile(ctx *quickjs.Context, path quickjs.Value, content quickjs.Value) quickjs.Value {
err := afero.WriteFile(fs.Fs, path.String(), []byte(content.String()), 0777)
tools.Check(err)
return ctx.Bool(true)
}
func (fs *FsDriver) Exists(ctx *quickjs.Context, path quickjs.Value) quickjs.Value {
data, err := afero.Exists(fs.Fs, path.String())
tools.Check(err)
return ctx.Bool(data)
}
func (fs *FsDriver) DirExists(ctx *quickjs.Context, path quickjs.Value) quickjs.Value {
data, err := afero.DirExists(fs.Fs, path.String())
tools.Check(err)
return ctx.Bool(data)
}
func (fs *FsDriver) Cwd(ctx *quickjs.Context) quickjs.Value {
dir, err := os.Getwd()
tools.Check(err)
return ctx.String(dir)
}
type FileInfo struct {
Name string
Size int64
Mode os.FileMode
ModTime time.Time
IsDir bool
}
func (fs *FsDriver) Stat(ctx *quickjs.Context, path quickjs.Value) quickjs.Value {
entry, err := fs.Fs.Stat(path.String())
tools.Check(err)
f := FileInfo{
Name: entry.Name(),
Size: entry.Size(),
Mode: entry.Mode(),
ModTime: entry.ModTime(),
IsDir: entry.IsDir(),
}
output, err := json.Marshal(f)
tools.Check(err)
return ctx.String(string(output))
}
func (fs *FsDriver) Remove(ctx *quickjs.Context, path quickjs.Value) quickjs.Value {
err := fs.Fs.Remove(path.String())
tools.Check(err)
return ctx.Bool(true)
}
func (fs *FsDriver) Mkdir(ctx *quickjs.Context, path quickjs.Value) quickjs.Value {
err := fs.Fs.Mkdir(path.String(), os.FileMode(0777))
tools.Check(err)
return ctx.Bool(true)
}
type walkFs struct {
Name string
Size int64
Mode os.FileMode
ModTime time.Time
IsDir bool
Path string
}
func (fs *FsDriver) Walk(ctx *quickjs.Context, pathDir quickjs.Value) quickjs.Value {
var files []walkFs
err := filepath.Walk(pathDir.String(), func(path string, info os.FileInfo, err error) error {
data := walkFs{
Name: info.Name(),
Size: info.Size(),
Mode: info.Mode(),
ModTime: info.ModTime(),
IsDir: info.IsDir(),
Path: path,
}
if err != nil {
tools.Check(err)
}
files = append(files, data)
return nil
})
if err != nil {
tools.Check(err)
}
output, err := json.Marshal(files)
tools.Check(err)
return ctx.String(string(output))
}