forked from kardianos/govendor
/
license.go
143 lines (123 loc) · 3.24 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
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package context
import (
"os"
"path/filepath"
"strings"
"github.com/kardianos/govendor/internal/pathos"
)
type licenseSearchType byte
const (
licensePrefix licenseSearchType = iota
licenseSubstring
licenseSuffix
)
type license struct {
Text string
Search licenseSearchType
}
func (t licenseSearchType) Test(filename, test string) bool {
switch t {
case licensePrefix:
return strings.HasPrefix(filename, test)
case licenseSubstring:
return strings.Contains(filename, test)
case licenseSuffix:
return strings.HasSuffix(filename, test)
}
return false
}
type licenseTest interface {
Test(filename, test string) bool
}
// licenses lists the filenames to copy over to the vendor folder.
var licenses = []license{
{Text: "license", Search: licensePrefix},
{Text: "unlicense", Search: licensePrefix},
{Text: "copying", Search: licensePrefix},
{Text: "copyright", Search: licensePrefix},
{Text: "copyright", Search: licensePrefix},
{Text: "legal", Search: licenseSubstring},
{Text: "notice", Search: licenseSubstring},
{Text: "disclaimer", Search: licenseSubstring},
{Text: "patent", Search: licenseSubstring},
{Text: "third-party", Search: licenseSubstring},
{Text: "thirdparty", Search: licenseSubstring},
}
func isLicenseFile(name string) bool {
cname := strings.ToLower(name)
for _, L := range licenses {
if L.Search.Test(cname, L.Text) {
return true
}
}
return false
}
// licenseCopy starts the search in the parent of "startIn" folder.
// Looks in all sub-folders until root is reached. The root itself is not
// searched.
func licenseCopy(root, startIn, vendorRoot, pkgPath string) error {
addTo, _ := pathos.TrimCommonSuffix(pathos.SlashToFilepath(pkgPath), startIn)
folder := filepath.Clean(filepath.Join(startIn, ".."))
for i := 0; i <= looplimit; i++ {
dir, err := os.Open(folder)
if err != nil {
return err
}
fl, err := dir.Readdir(-1)
dir.Close()
if err != nil {
return err
}
for _, fi := range fl {
name := fi.Name()
if name[0] == '.' {
continue
}
if fi.IsDir() {
continue
}
if !isLicenseFile(name) {
continue
}
srcPath := filepath.Join(folder, name)
trimTo := pathos.FileTrimPrefix(getLastVendorRoot(folder), root)
/*
Path: "golang.org/x/tools/go/vcs"
Root: "/tmp/govendor-cache280388238/1"
StartIn: "/tmp/govendor-cache280388238/1/go/vcs"
addTo: "golang.org/x/tools"
$PROJ/vendor + addTo + pathos.FileTrimPrefix(folder, root) + "LICENSE"
*/
destPath := filepath.Join(vendorRoot, addTo, trimTo, name)
// Only copy if file does not exist.
_, err := os.Stat(destPath)
if err == nil {
continue
}
err = copyFile(destPath, srcPath, nil)
if err != nil {
return err
}
}
if len(folder) <= len(root) {
return nil
}
nextFolder := filepath.Clean(filepath.Join(folder, ".."))
if nextFolder == folder {
return nil
}
folder = nextFolder
}
panic("copyLicense loop limit")
}
func getLastVendorRoot(s string) string {
w := strings.Replace(s, "\\", "/", -1)
ix := strings.LastIndex(w, "/vendor/")
if ix < 0 {
return s
}
return s[ix+len("/vendor"):]
}