-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (66 loc) · 1.7 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
package main
import (
"io"
"io/ioutil"
"log"
"os"
"path"
"runtime/pprof"
classifier "github.com/google/licenseclassifier/v2"
"github.com/google/licenseclassifier/v2/assets"
)
var cf *classifier.Classifier
type LicenseFinding string
func handleAllLicenses() {
//const folder = "./licenses"
const folder = "perl-licenses"
files, err := ioutil.ReadDir(folder)
if err != nil {
log.Fatalf("can't ReadDir %q. error: %v", folder, err)
}
for _, f := range files {
lic, err := os.Open(path.Join(folder, f.Name()))
if err != nil {
log.Printf("[ERROR] can't open %q: %v", f.Name(), err)
continue
}
Classify(lic)
}
}
func main() {
var err error
cf, err = assets.DefaultClassifier()
if err != nil {
panic("assets.DefaultClassifier: " + err.Error())
}
// lic, err := os.Open("./licenses/libssl1.1")
// Classify(lic)
handleAllLicenses()
fMem, err := os.Create("mem.profile")
if err != nil {
panic("could not create memory profile: " + err.Error())
}
defer fMem.Close() // error handling omitted for example
if err := pprof.WriteHeapProfile(fMem); err != nil {
panic("could not write memory profile: " + err.Error())
}
}
// Classify uses a single classifier to detect and classify the license found in a file
func Classify(r io.Reader) {
// Use 'github.com/google/licenseclassifier' to find licenses
result, err := cf.MatchFrom(r)
if err != nil {
log.Fatalf("unable to match licenses: %v", err)
}
var findings []LicenseFinding
seen := map[string]struct{}{}
for _, match := range result.Matches {
if match.Confidence <= 0.9 {
continue
}
if _, ok := seen[match.Name]; !ok {
findings = append(findings, LicenseFinding(match.Name))
seen[match.Name] = struct{}{}
}
}
}