forked from src-d/go-license-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.go
74 lines (64 loc) · 1.56 KB
/
analysis.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
package licensedb
import (
"net/url"
"os"
"sort"
"sync"
"github.com/chen-keinan/go-license-detector/licensedb/filer"
)
// Analyse runs license analysis on each item in `args`
func Analyse(args ...string) []Result {
nargs := len(args)
results := make([]Result, nargs)
var wg sync.WaitGroup
wg.Add(nargs)
for i, arg := range args {
go func(i int, arg string) {
defer wg.Done()
matches, err := process(arg)
res := Result{Arg: arg, Matches: matches}
if err != nil {
res.ErrStr = err.Error()
}
results[i] = res
}(i, arg)
}
wg.Wait()
return results
}
// Result gathers license detection results for a project path
type Result struct {
Arg string `json:"project,omitempty"`
Matches []Match `json:"matches,omitempty"`
ErrStr string `json:"error,omitempty"`
}
// Match describes the level of confidence for the detected License
type Match struct {
License string `json:"license"`
Confidence float32 `json:"confidence"`
}
func process(arg string) ([]Match, error) {
newFiler := filer.FromDirectory
fi, err := os.Stat(arg)
if err != nil {
if _, err := url.Parse(arg); err == nil {
newFiler = filer.FromGitURL
}
} else if !fi.IsDir() {
newFiler = filer.FromSiva
}
resolvedFiler, err := newFiler(arg)
if err != nil {
return nil, err
}
ls, err := Detect(resolvedFiler)
if err != nil {
return nil, err
}
var matches []Match
for k, v := range ls {
matches = append(matches, Match{k, v.Confidence})
}
sort.Slice(matches, func(i, j int) bool { return matches[i].Confidence > matches[j].Confidence })
return matches, nil
}