forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
56 lines (45 loc) · 1.08 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
package fs
import (
"os"
"path/filepath"
"github.com/yaoapp/gou/fs"
"github.com/yaoapp/gou/fs/dsl"
"github.com/yaoapp/gou/fs/system"
"github.com/yaoapp/yao/config"
)
// Load system fs
func Load(cfg config.Config) error {
root, err := filepath.Abs(cfg.Root)
if err != nil {
return err
}
dataRoot, err := Root(cfg)
if err != nil {
return err
}
scriptRoot := filepath.Join(root, "scripts")
dslDenyList := []string{scriptRoot, dataRoot}
if _, err := os.Stat(dataRoot); os.IsNotExist(err) {
err := os.MkdirAll(dataRoot, os.ModePerm)
if err != nil {
return err
}
}
fs.Register("system", system.New(dataRoot))
fs.Register("dsl", dsl.New(root).DenyAbs(dslDenyList...)) // DSL
fs.Register("script", system.New(scriptRoot)) // Script
fs.Register("binary", system.New(root)) // Binary
return nil
}
// Root return data root
func Root(cfg config.Config) (string, error) {
root := cfg.DataRoot
if root == "" {
root = filepath.Join(cfg.Root, "data")
}
root, err := filepath.Abs(root)
if err != nil {
return "", err
}
return root, nil
}