forked from google/go-licenses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
finder.go
125 lines (105 loc) · 3.14 KB
/
finder.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package chklicenses
import (
"context"
"flag"
"fmt"
"io/ioutil"
"strings"
"github.com/google/licenseclassifier"
"github.com/markbates/pkger"
"github.com/hashicorp/go-multierror"
"github.com/khulnasoft/chk-licenses/chklicenses/licenses"
)
type LicenseFinder struct {
Paths []string
ConfidenceThreshold float64
GitRemotes []string
}
func NewLicenseFinder(paths, gitRemotes []string, threshold float64) LicenseFinder {
return LicenseFinder{
Paths: paths,
GitRemotes: gitRemotes,
ConfidenceThreshold: threshold,
}
}
func licenseDBArchiveFetcher() ([]byte, error) {
f, err := pkger.Open("/assets/licenses.db")
if err != nil {
return nil, fmt.Errorf("unable to open license.db: %w", err)
}
defer f.Close()
return ioutil.ReadAll(f)
}
func (r LicenseFinder) Find() (<-chan LicenseResult, error) {
// suppress log events from go-licenses
flag.Parse()
_ = flag.Lookup("logtostderr").Value.Set("false")
dbFetcherOpt := licenseclassifier.ArchiveFunc(licenseDBArchiveFetcher)
classifier, err := licenses.NewClassifier(r.ConfidenceThreshold, dbFetcherOpt)
if err != nil {
return nil, err
}
libs, err := licenses.Libraries(context.Background(), classifier, r.Paths...)
if err != nil {
return nil, err
}
results := make(chan LicenseResult)
go func() {
defer close(results)
for _, lib := range libs {
var licenseURL, licenseName string
var classification licenses.Type
var errs error
if lib.LicensePath != "" {
licenseURL, err = findLicenseURL(lib, r.GitRemotes...)
if err != nil {
errs = multierror.Append(errs, fmt.Errorf("failed to locate license URL (%s): %w", lib.LicensePath, err))
licenseURL = ""
}
licenseName, classification, err = classifier.Identify(lib.LicensePath)
if err != nil {
errs = multierror.Append(errs, fmt.Errorf("failed to identify license (%s): %w", lib.LicensePath, err))
licenseName = ""
}
}
results <- LicenseResult{
Library: unvendor(lib.Name()),
URL: licenseURL,
Path: lib.LicensePath,
License: licenseName,
Type: classification.String(),
Errs: errs,
}
}
}()
return results, nil
}
func findLicenseURL(lib *licenses.Library, gitRemotes ...string) (string, error) {
// find a URL for the license file, based on the URL of a remote for the git repository.
repo, err := licenses.FindGitRepo(lib.LicensePath)
if err != nil {
// can't find git repo (possibly a go module?) - derive URL from lib name instead.
lURL, err := lib.FileURL(lib.LicensePath)
if err != nil {
return "", err
}
return lURL.String(), nil
}
var errs error
for _, remote := range gitRemotes {
url, err := repo.FileURL(lib.LicensePath, remote)
if err != nil {
errs = multierror.Append(errs, err)
continue
}
return url.String(), errs
}
return "", multierror.Append(errs, fmt.Errorf("failed to find license URL"))
}
func unvendor(importPath string) string {
// Remove the "*/vendor/" prefix from the library name for conciseness.
if vendorerAndVendoree := strings.SplitN(importPath, "/vendor/", 2); len(vendorerAndVendoree) == 2 {
return vendorerAndVendoree[1]
}
return importPath
}