Parcourir la Source

added interval and some minor refactoring

bmallred 9 ans auparavant
Parent
Commettre
3709699b5b
1 fichiers modifiés avec 27 ajouts et 12 suppressions
  1. 27 12
      code-notify.go

+ 27 - 12
code-notify.go

@ -11,22 +11,40 @@ import (
11 11
	"regexp"
12 12
	"runtime"
13 13
	"strings"
14
	"time"
14 15
15 16
	"code.revolvingcow.com/revolvingcow/code/cmd"
16 17
)
17 18
18 19
// Main entry point for the application.
19 20
func main() {
21
	// Configure any environment variables that may need to be applied
20 22
	cmd.ConfigureEnvironment()
21 23
24
	// Find the root directory to start the hunt
22 25
	root := cmd.GetWorkingDirectory()
23 26
	if len(os.Args[1:]) > 0 {
24 27
		root = os.Args[1:][0]
25 28
	}
26
	c := hunter(root)
27 29
28
	for m := range c {
29
		gatherer(m)
30
	// Loop through the repositories on an interval
31
	interval := 15 * time.Minute
32
	doEvery(interval, func() {
33
		c := hunter(root)
34
35
		for m := range c {
36
			gatherer(m)
37
		}
38
	})
39
}
40
41
// Repeat a piece of logic on an interval
42
func doEvery(d time.Duration, f func()) {
43
	log.Println("Execution scheduled every", d)
44
45
	for _ = range time.Tick(d) {
46
		log.Println("Scanning repositories for incoming changesets...")
47
		f()
30 48
	}
31 49
}
32 50
@ -81,30 +99,27 @@ func gatherer(directory string) {
81 99
82 100
	err := app.Run()
83 101
	if err == nil {
84
		count := 0
85 102
		base := path.Base(directory)
86 103
		b := buffer.String()
87 104
88 105
		// Special logic to count the individual changesets
106
		var re *regexp.Regexp
89 107
		switch {
90 108
		case base == ".git":
91
			re := regexp.MustCompile("commit")
92
			count = len(re.FindAllString(b, -1))
109
			re = regexp.MustCompile("commit")
93 110
			break
94 111
		case base == ".hg":
95
			re := regexp.MustCompile("changeset")
96
			count = len(re.FindAllString(b, -1))
112
			re = regexp.MustCompile("changeset")
97 113
			break
98 114
		case base == ".tf":
99
			re := regexp.MustCompile("revno")
100
			count = len(re.FindAllString(b, -1))
115
			re = regexp.MustCompile("revno")
101 116
			break
102 117
		case base == ".bzr":
103
			re := regexp.MustCompile("commit")
104
			count = len(re.FindAllString(b, -1))
118
			re = regexp.MustCompile("commit")
105 119
			break
106 120
		}
107 121
122
		count := len(re.FindAllString(b, -1))
108 123
		if count > 0 {
109 124
			a := []string{}
110 125