-
Notifications
You must be signed in to change notification settings - Fork 8
/
license-finder.go
57 lines (51 loc) · 1.43 KB
/
license-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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/go-enry/go-license-detector/v4/licensedb"
"github.com/go-enry/go-license-detector/v4/licensedb/filer"
)
// copyLicenses finds license files in the provided directories,
// and copies them into the vendor directories of the stubbed packages.
func copyLicenses(licenseDirs []string) error {
if licenseDirs == nil {
return nil
}
for _, licenseSearchDir := range licenseDirs {
fl, err := filer.FromDirectory(licenseSearchDir)
if err != nil {
return err
}
licenses, err := licensedb.Detect(fl)
if err != nil {
return err
}
filenames := make(map[string]bool)
{
for _, match := range licenses {
for fName := range match.Files {
filenames[fName] = true
}
}
}
for licenseRelativePath := range filenames {
// Exclude licenses of vendored packages:
if strings.Contains(licenseRelativePath, "/vendor/") {
continue
}
licenseFilepath := filepath.Join(licenseSearchDir, licenseRelativePath)
dstFolder := filepath.Dir(*destination)
dstFilepath := filepath.Join(dstFolder, licenseRelativePath)
if strings.HasSuffix(dstFilepath, ".go") {
// When saving, add .txt extension.
dstFilepath += ".txt"
}
fmt.Println(fmt.Sprintf("Copying %s to %s", licenseFilepath, dstFilepath))
MustCreateFolderIfNotExists(filepath.Dir(dstFilepath), os.ModePerm)
MustCopyFile(licenseFilepath, dstFilepath)
}
}
return nil
}