-
Notifications
You must be signed in to change notification settings - Fork 1
/
sane.go
73 lines (59 loc) · 1.46 KB
/
sane.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
73
package sane
import (
"fmt"
"strings"
)
const (
REPOSITORY_PATH_PLACEHOLDER = "$REPOSITORY"
REPOSITORY_SANE_DEFAULT_FILE = ".sane"
)
// Primary entrypoint for validator
func Execute(config Config) error {
if strings.HasPrefix(config.RulesPath, REPOSITORY_PATH_PLACEHOLDER) {
config.RulesPath = strings.Replace(config.RulesPath,
REPOSITORY_PATH_PLACEHOLDER, config.RepositoryPath, 1)
}
Infof("Using Repository: %s", config.RepositoryPath)
Infof("Using Rule: %s", config.RulesPath)
Infof("Strict mode: %t", config.Strict)
Infof("Fail fast mode: %t", config.FailFast)
repository, err := newGitRepositoryWalker(config.RepositoryPath)
if err != nil {
return err
}
ruleEngine, err := newRuleEngine(config.RulesType,
config.RulesPath, config.Strict)
if err != nil {
return err
}
violatingPaths := []string{}
err = repository.Walk(func(node RepositoryNode) error {
ok, err := ruleEngine.Validate(node)
if err != nil {
return err
}
if !ok {
if config.FailFast {
return fmt.Errorf("fail-fast: no rules match for: %s", node.FullPath)
} else {
violatingPaths = append(violatingPaths, node.FullPath)
}
}
return nil
})
if err != nil {
return err
}
if len(violatingPaths) > 0 {
for _, path := range violatingPaths {
Infof("Failed: %s", path)
}
return fmt.Errorf("%d paths failed validation", len(violatingPaths))
}
err = ruleEngine.Finalize()
if err != nil {
return err
}
Infof("Finished validation")
return nil
}