-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (97 loc) · 1.96 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"errors"
"log"
"os"
"os/signal"
"signachurn/db"
"signachurn/scan"
"signachurn/scan/cpp"
"signachurn/scan/proto"
"syscall"
"time"
)
type Scanners struct {
all []scan.Scanner
}
func NewScanners() *Scanners {
result := new(Scanners)
result.all = make([]scan.Scanner, 0)
return result
}
func (s *Scanners) Process(job *proto.ScanJob) ([]*proto.ScanResult, error) {
for _, sc := range s.all {
if sc.Accepts(job) {
res, err := sc.Scan(job)
if err != nil {
if _, ok := err.(*scan.NoValidContentError); ok {
continue
}
return res, err
}
return res, nil
}
}
return nil, &scan.NoScannerForJobError{
Err: errors.New("no scanner for job"),
}
}
func HandleSigTerm(cb func(sig os.Signal)) {
signalChannel := make(chan os.Signal, 2)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-signalChannel
switch sig {
case os.Interrupt:
cb(sig)
case syscall.SIGTERM:
cb(sig)
}
}()
}
func main() {
HandleSigTerm(func(sig os.Signal) {
os.Exit(0)
})
log.Println("starting DB")
db := db.NewDB()
db.Start(func() {
log.Println("init scanners")
scanners := NewScanners()
scanners.all = append(scanners.all, &cpp.ScannerCpp{})
log.Println("adding job")
job := &proto.ScanJob{
Type: proto.ScanJobType_SCAN_JOB_GIT,
Git: &proto.ScanJobGit{
RemoteURL: "https://github.com/glfw/glfw",
},
}
log.Println("process job")
results, err := scanners.Process(job)
if err != nil {
panic(err)
}
repoId, err := db.EnsureRepo(job.Git.RemoteURL)
if err != nil {
panic(err)
}
for _, result := range results {
sigs := result.GetSignatures()
tagName := result.TagName
log.Println("Tag:", tagName, "-> found", len(sigs), "signatures")
err, sigIds := db.AddSignatures(sigs...)
if err != nil {
// panic(err)
}
err = db.AddTag(
repoId,
tagName,
sigIds...,
)
}
})
defer db.Stop()
for {
time.Sleep(time.Millisecond * 25)
}
}