-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlicense.go
160 lines (138 loc) · 3.12 KB
/
license.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"go/build"
"os"
"path/filepath"
"sort"
"strings"
"github.com/google/licensecheck"
"golang.org/x/mod/module"
)
type license struct {
// Content holds the license content
Content []byte
// Name holds the license file name (i.e. LICENSE.md)
Name string
// Type holds the license type (i.e. MIT)
Type string
module.Version
}
func (l license) ModuleVersion() string {
return l.Version.Path + "@" + l.Version.Version
}
func (l license) ModulePath() (string, error) {
epath, err := module.EscapePath(l.Version.Path)
if err != nil {
return "", fmt.Errorf("invalid module path: %s", err)
}
return epath + "@" + l.Version.Version, nil
}
func (l license) LicensePath() (string, error) {
modpath, err := l.ModulePath()
if err != nil {
return "", err
}
return filepath.Join(modpath, l.Name), nil
}
var licenseNames = []string{
"COPYING",
"COPYING.MD",
"COPYING.TXT",
"LICENSE",
"LICENSE.MD",
"LICENSE.TXT",
}
func getGoModCache() string {
gomodcache := os.Getenv("GOMODCACHE")
if gomodcache != "" {
return gomodcache
}
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
return filepath.Join(gopath, "pkg", "mod")
}
func getLicenses(gomodcache string, info moduleInfo, patterns []string, excluded map[string]struct{}) ([]license, error) {
licenses := []license{}
for _, v := range info.Require {
l := license{
Version: v,
}
modpath, err := l.ModulePath()
if err != nil {
return nil, err
}
if _, ok := excluded[modpath]; ok {
continue
}
licenseFiles, err := findLicenses(filepath.Join(gomodcache, modpath), patterns)
if err != nil {
return nil, fmt.Errorf("could not scan for licenses: %w", err)
}
if len(licenseFiles) == 0 {
return nil, fmt.Errorf("license not found for %s in %s", v.String(), modpath)
}
for _, name := range licenseFiles {
l := l
l.Name = name
licensePath, err := l.LicensePath()
if err != nil {
return nil, err
}
data, err := os.ReadFile(filepath.Join(gomodcache, licensePath))
if err != nil {
return nil, fmt.Errorf("error reading license file: %w", err)
}
coverage := licensecheck.Scan(data)
for _, match := range coverage.Match {
l.Content = data
l.Type = match.ID
licenses = append(licenses, l)
}
}
}
return licenses, nil
}
func findLicenses(path string, licenseFileNames []string) ([]string, error) {
licenses := []string{}
entries, err := os.ReadDir(path)
if err != nil {
return licenses, err
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
upperFileName := strings.ToUpper(entry.Name())
for _, v := range licenseFileNames {
if upperFileName == v {
licenses = append(licenses, entry.Name())
break
}
}
}
return licenses, nil
}
func listLicenses() {
m := map[string]bool{}
licenses := []string{}
for _, v := range licensecheck.BuiltinLicenses() {
l := v.ID
if _, ok := m[l]; ok {
continue
}
m[l] = true
licenses = append(licenses, l)
}
sort.Strings(licenses)
for _, v := range licenses {
fmt.Println(v)
}
}
func listLicenseNames() {
for _, v := range licenseNames {
fmt.Println(v)
}
}