-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (85 loc) · 1.97 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
)
func main() {
// Parse command line options
concurrency := flag.Int("concurrency", 8, "How many goroutines")
cfgGlob := flag.String("config", "", "Config file(s) to process")
rspamdURL := flag.String("url", "http://127.0.0.1:11333/checkv2", "Rspamd URL")
flag.Parse()
// Use context to stop goroutines
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Count tests
total := 0
totalPassed := 0
totalFailed := 0
defer func() {
fmt.Println(fmt.Sprintf("\n%d tests processed, %d passed, %d failed", total, totalPassed, totalFailed))
}()
// Configs discovered by worker
configCh := make(chan Config, 1)
// Jobs compiled from configs
jobCh := make(chan Job, 1)
// Results of jobs
resultCh := make(chan Result, 1)
// OS signals
sigCh := make(chan os.Signal, 1)
// createJobs creates a task for each file to be scanned
go createJobs(ctx, configCh, jobCh)
// processJobs runs the individual jobs
go func() {
var wg sync.WaitGroup
for i := 0; i < *concurrency; i++ {
wg.Add(1)
go processJobs(ctx, jobCh, resultCh, &wg, *rspamdURL)
}
wg.Wait()
close(resultCh)
}()
// Catch interrupt signal
signal.Notify(sigCh, os.Interrupt)
// Process config files
matches, err := filepath.Glob(*cfgGlob)
if err != nil {
fmt.Println("Couldn't read config: %s", err.Error())
return
}
for _, match := range matches {
realCfg, err := ReadConfig(match)
if err != nil {
fmt.Println("Couldn't parse config(%s): %s", match, err)
return
}
for _, cfg := range realCfg.Tests {
configCh <- cfg
}
}
close(configCh)
// Wait to die
for {
select {
case <-sigCh:
return
case result, ok := <-resultCh:
if !ok {
return
}
if !result.Passed {
fmt.Println(fmt.Sprintf("FAILED: %s(%s): %s", result.Name, result.File, strings.Join(result.Errors, ",")))
totalFailed++
} else {
totalPassed++
}
total++
}
}
}