-
Notifications
You must be signed in to change notification settings - Fork 6
/
find.go
72 lines (63 loc) · 1.87 KB
/
find.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
package main
// A cmd that searches for all the *.keel files in the current working dir and
// any subdirectories (recursively), and
// reports on schema files found whose contents match some condition (of your choice).
//
// It helped when we switched the default permission to no-permission and thus
// had to edit all our existing schemas, but not in a perfectly universal way.
// And will likely be useful again for something similar.
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
func main() {
cwd, err := os.Getwd()
panicOnErr(err)
err = filepath.WalkDir(cwd, func(path string, d os.DirEntry, unused error) error {
if d.IsDir() {
return nil
}
fName := d.Name()
if !strings.HasSuffix(strings.ToLower(fName), ".keel") {
return nil
}
b, err := os.ReadFile(path)
panicOnErr(err)
schemaAsString := string(b)
lines := strings.Split(schemaAsString, "\n")
applyTests(path, lines, schemaAsString)
return nil
})
panicOnErr(err)
}
func panicOnErr(err error) {
if err == nil {
return
}
panic(fmt.Sprintf("stopping on err: %s", err))
}
// You can change this function to suit your purpose.
func applyTests(filePath string, lines []string, schemaAsString string) {
// Example 1) capture the location of all schema files that have at least one references to permissions?
if strings.Contains(schemaAsString, "@permission") {
fmt.Printf("XXXX This file uses permission attribute: %s\n", filePath)
}
// Example 2) capture the first line of every model declaration in all schema files.
modelLines := []string{}
re := regexp.MustCompile(`model\s`)
for _, line := range lines {
matched := re.MatchString(line)
if matched {
modelLines = append(modelLines, line)
}
}
if len(modelLines) > 0 {
fmt.Printf("XXXX this file contains the following model definitions\n%s\n", filePath)
for _, ln := range modelLines {
fmt.Printf(" %s\n", ln)
}
}
}