forked from bpicode/fritzctl
/
resolver_license_file.go
65 lines (56 loc) · 1.67 KB
/
resolver_license_file.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
package main
import (
"fmt"
"go/build"
"os"
"path/filepath"
"runtime"
"strings"
)
var licenseFiles = []string{"LICENSE", "LICENSE.md", "LICENSE.txt", "LICENSE.rst", "COPYING", "License", "MIT-LICENSE.txt"}
type licenceFileResolver struct {
}
func (r licenceFileResolver) resolve(ps ...pkg) ([]pkgWithLicenseFile, []error) {
pswlf := make([]pkgWithLicenseFile, len(ps))
errs := make([]error, len(ps))
for i, p := range ps {
ptmp := p
withLic := pkgWithLicenseFile{}
withLic.pkg = &ptmp
withLic.licFilePath, errs[i] = r.resolveOne(p)
pswlf[i] = withLic
}
return pswlf, errs
}
func (r licenceFileResolver) resolveOne(p pkg) (string, error) {
if len(p.GoFiles) == 0 {
return "", fmt.Errorf("no go files for package '%v', don't know where to search for license", p.PkgPath)
}
startingDir := filepath.Dir(p.GoFiles[0])
for dir := startingDir; !strings.HasSuffix(dir, string(os.PathSeparator)) && r.stillInScope(dir, p.PkgPath); dir = filepath.Dir(dir) {
path := r.searchLicenseFile(dir)
if path != "" {
return path, nil
}
}
return "", fmt.Errorf("no license file found, starting from '%s' (have tried %v)", p.GoFiles[0], licenseFiles)
}
func (r licenceFileResolver) stillInScope(dir string, pkgPath string) bool {
gp := strings.Index(dir, os.Getenv("GOPATH"))
dgp := strings.Index(dir, build.Default.GOPATH)
gr := strings.Index(dir, runtime.GOROOT())
if gp >= 0 || dgp >= 0 || gr >= 0 {
return true
}
return strings.Contains(dir, pkgPath)
}
func (r licenceFileResolver) searchLicenseFile(dir string) string {
for _, name := range licenseFiles {
path := filepath.Join(dir, name)
_, err := os.Stat(path)
if err == nil {
return path
}
}
return ""
}