-
Notifications
You must be signed in to change notification settings - Fork 16
/
mkdir.go
57 lines (49 loc) · 1.3 KB
/
mkdir.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
package dynaml
import (
"os"
"path/filepath"
"github.com/mandelsoft/spiff/yaml"
)
func init() {
RegisterFunction("mkdir", func_mkdir)
}
func func_mkdir(arguments []interface{}, binding Binding) (interface{}, EvaluationInfo, bool) {
var err error
info := DefaultInfo()
wopt := WriteOpts{
Permissions: 0755,
}
if !binding.GetState().FileAccessAllowed() {
return info.DenyOSOperation("mkdir")
}
if len(arguments) < 1 || len(arguments) > 2 {
return info.Error("mkdir requires one or two arguments")
}
path, _, err := getArg(0, arguments[0], wopt, false)
if err != nil {
comps, ok := arguments[0].([]yaml.Node)
if !ok {
return info.Error("path argument must be a non-empty string")
}
for i, a := range comps {
comp, _, err := getArg(i, a.Value(), wopt, false)
if err != nil || comp == "" {
return info.Error("path component %d must be a non-empty string", i)
}
path = filepath.Join(path, comp)
}
}
if path == "" {
return info.Error("path argument must not be empty")
}
if len(arguments) == 2 {
wopt, err = getWriteOptions(arguments[1], wopt, true)
}
if err == nil {
err = binding.GetState().FileSystem().MkdirAll(path, os.FileMode(wopt.Permissions))
if err == nil {
return path, info, true
}
}
return info.Error("cannot create directory %q: %s", path, err)
}