-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrewrite_config.go
56 lines (44 loc) · 940 Bytes
/
rewrite_config.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 zanzibar
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
)
const configExt = "yaml"
// LoadRewriteRules gets rules from all yaml files inside the given directory
func LoadRewriteRules(root string) (map[string]UsersetRewrite, error) {
res := make(map[string]UsersetRewrite)
if root == "" {
return res, nil
}
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
index := strings.LastIndex(path, ".")
if index == -1 {
return nil
}
ext := path[index+1:]
if ext != configExt {
return nil
}
blob, err := ioutil.ReadFile(path)
if err != nil {
return err
}
var t NamespaceConfig
err = yaml.Unmarshal(blob, &t)
if err != nil {
return err
}
namespace := t.Name
for _, rel := range t.Relations {
res[namespace+"#"+rel.Name] = rel.UsersetRewrite
}
return nil
})
return res, err
}