-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
validator.go
67 lines (58 loc) 路 1021 Bytes
/
validator.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
package main
import (
"fmt"
"io/fs"
"path/filepath"
"strings"
)
var (
dir string
level int
)
type ValidationResponse struct {
Level int
Valid bool
HasEditions bool
}
func validateCollection(dir string) ValidationResponse {
level = 0
dir, _ = filepath.Abs(dir)
err := filepath.WalkDir(dir, dirwalker)
if err != nil {
panic(err)
}
return ValidationResponse{
Level: level,
Valid: level == 1 || level == 2,
HasEditions: level == 2,
}
}
func getlevel(root string, dir string) int {
fmt.Println("Root ", root)
fmt.Println("Dir ", dir)
s := strings.Replace(dir, root, "", 1)
fmt.Println("Clean Dir ", s)
l := strings.Split(s, "/")
filtered := []string{}
for _, v := range l {
if len(v) > 0 {
filtered = append(filtered, v)
}
}
return len(filtered)
}
func dirwalker(p string, i fs.DirEntry, err error) error {
if err != nil {
panic(err)
}
if i.IsDir() {
if dir == "" {
dir = p
}
l := getlevel(dir, p)
if l > level {
level = l
}
}
return nil
}