forked from thoughtworks/talisman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner.go
59 lines (48 loc) · 1.48 KB
/
runner.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
package main
import (
"fmt"
"os"
"github.com/thoughtworks/talisman/detector"
"github.com/thoughtworks/talisman/git_repo"
)
const (
//CompletedSuccessfully is an exit status that says that the current runners run completed without errors
CompletedSuccessfully int = 0
//CompletedWithErrors is an exit status that says that the current runners run completed with failures
CompletedWithErrors int = 1
)
//Runner represents a single run of the validations for a given commit range
type Runner struct {
additions []git_repo.Addition
results *detector.DetectionResults
}
//NewRunner returns a new Runner.
func NewRunner(additions []git_repo.Addition) *Runner {
return &Runner{additions, detector.NewDetectionResults()}
}
//RunWithoutErrors will validate the commit range for errors and return either COMPLETED_SUCCESSFULLY or COMPLETED_WITH_ERRORS
func (r *Runner) RunWithoutErrors() int {
r.doRun()
r.printReport()
return r.exitStatus()
}
func (r *Runner) doRun() {
ignores := detector.ReadIgnoresFromFile(readRepoFile())
detector.DefaultChain().Test(r.additions, ignores, r.results)
}
func (r *Runner) printReport() {
if r.results.HasIgnores() || r.results.HasFailures() {
fmt.Println(r.results.Report())
}
}
func (r *Runner) exitStatus() int {
if r.results.HasFailures() {
return CompletedWithErrors
}
return CompletedSuccessfully
}
func readRepoFile() func(string) ([]byte, error) {
wd, _ := os.Getwd()
repo := git_repo.RepoLocatedAt(wd)
return repo.ReadRepoFileOrNothing
}