-
Notifications
You must be signed in to change notification settings - Fork 1
/
files.go
177 lines (146 loc) · 3.96 KB
/
files.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
170
171
172
173
174
175
176
177
package kmodules
import (
"github.com/relationsone/gomini"
"reflect"
"os"
"path/filepath"
"github.com/spf13/afero"
)
type files string
const kmoduleFiles files = "0c97cffa-f27f-49f9-95cf-5472a98253a9"
func NewFilesModule() gomini.KernelModule {
return kmoduleFiles
}
func (f files) ID() string {
return string(f)
}
func (files) Name() string {
return "files"
}
func (files) ApiDefinitionFile() string {
return "/kernel/@types/files"
}
func (files) SecurityInterceptor() gomini.SecurityInterceptor {
return func(caller gomini.Bundle, property string) bool {
// Default checks are enough
return true
}
}
func (files) KernelModuleBinder() gomini.KernelModuleBinder {
return func(bundle gomini.Bundle, builder gomini.ObjectBuilder) {
resolve := func(ppath string) (*path, error) {
info, err := bundle.Filesystem().Stat(ppath)
if !os.IsNotExist(err) {
return nil, err
}
filetype := ft_unknown
if info != nil {
if info.IsDir() {
filetype = ft_directory
} else if gomini.IsKernelFile(bundle.Filesystem(), ppath) {
filetype = ft_kernel
} else {
filetype = ft_file
}
}
return &path{
name: filepath.Base(ppath),
path: ppath,
bundle: bundle,
filetype: filetype,
}, nil
}
builder.DefineFunction("resolvePath", "resolvePath", func(call gomini.FunctionCall) gomini.Value {
if len(call.Arguments) < 1 {
return bundle.NewTypeError("illegal number of arguments")
}
val := call.Argument(0)
if val.ExportType().Kind() != reflect.String {
return bundle.NewTypeError("illegal parameter type")
}
p := val.String()
path, err := resolve(p)
if err != nil {
return bundle.NewTypeError(err)
}
return path.adapt(resolve)
})
}
}
type filetype int8
const (
ft_unknown filetype = 1
ft_kernel filetype = 2
ft_directory filetype = 4
ft_file filetype = 8
)
type path struct {
name string
path string
filetype filetype
bundle gomini.Bundle
}
func (p *path) exists(call gomini.FunctionCall) gomini.Value {
exists, err := afero.Exists(p.bundle.Filesystem(), p.path)
if err != nil {
return p.bundle.NewTypeError(err)
}
return p.bundle.ToValue(exists)
}
func (p *path) mkdir(call gomini.FunctionCall) gomini.Value {
if len(call.Arguments) < 1 {
return p.bundle.NewTypeError("illegal number of arguments")
}
val := call.Argument(0)
if val.ExportType().Kind() != reflect.Bool {
return p.bundle.NewTypeError("illegal parameter type")
}
var err error
if val.ToBoolean() {
err = p.bundle.Filesystem().MkdirAll(p.path, os.ModePerm)
} else {
err = p.bundle.Filesystem().Mkdir(p.path, os.ModePerm)
}
if err != nil {
return p.bundle.NewTypeError(err)
}
return p.bundle.Undefined()
}
func (p *path) resolve(resolve func(string) (*path, error)) func(gomini.FunctionCall) gomini.Value {
return func(call gomini.FunctionCall) gomini.Value {
if len(call.Arguments) < 1 {
return p.bundle.NewTypeError("illegal number of arguments")
}
val := call.Argument(0)
if val.ExportType().Kind() != reflect.String {
return p.bundle.NewTypeError("illegal parameter type")
}
ppath := val.String()
path, err := resolve(filepath.Join(p.path, ppath))
if err != nil {
return p.bundle.NewTypeError(err)
}
return path.adapt(resolve)
}
}
func (p *path) toFile(call gomini.FunctionCall) gomini.Value {
// TODO
return p.bundle.Undefined()
}
func (p *path) toPipe(call gomini.FunctionCall) gomini.Value {
// TODO
return p.bundle.Undefined()
}
func (p *path) adapt(resolve func(string) (*path, error)) gomini.Object {
builder := p.bundle.NewObjectBuilder("path")
builder.
DefineConstant("name", p.name).
DefineConstant("path", p.path).
DefineConstant("type", p.filetype).
DefineFunction("exists", "exists", p.exists).
DefineFunction("mkdir", "mkdir", p.mkdir).
DefineFunction("resolve", "resolve", p.resolve(resolve)).
DefineFunction("toFile", "toFile", p.toFile).
DefineFunction("toPipe", "toPipe", p.toPipe)
return builder.Build()
}