-
Notifications
You must be signed in to change notification settings - Fork 16
/
files.go
42 lines (34 loc) · 1007 Bytes
/
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
package dynaml
import (
"github.com/mandelsoft/spiff/yaml"
)
func func_listFiles(directory bool, arguments []interface{}, binding Binding) (interface{}, EvaluationInfo, bool) {
info := DefaultInfo()
if !binding.GetState().FileAccessAllowed() {
return info.DenyOSOperation("listFiles")
}
if len(arguments) != 1 {
return info.Error("list requires exactly one arguments")
}
name, ok := arguments[0].(string)
if !ok {
return info.Error("list: argument must be a string")
}
if name == "" {
return info.Error("list: argument is empty string")
}
if !checkExistence(binding, name, true) {
return info.Error("list: %q is no directory or does not exist", name)
}
files, err := binding.GetState().FileSystem().ReadDir(name)
if err != nil {
return info.Error("list: %q: error reading directory", name, err)
}
result := []yaml.Node{}
for _, f := range files {
if f.IsDir() == directory {
result = append(result, NewNode(f.Name(), binding))
}
}
return result, info, true
}