forked from gojp/goreportcard
/
license.go
62 lines (51 loc) · 1.25 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
package check
import (
"io/ioutil"
"path/filepath"
"strings"
)
// License is the check for the existence of a license file
type License struct {
Dir string
Filenames []string
}
// Name returns the name of the display name of the command
func (g License) Name() string {
return "license"
}
// Weight returns the weight this check has in the overall average
func (g License) Weight() float64 {
return .05
}
// thank you https://github.com/ryanuber/go-license and client9
var licenses = []string{
"license",
"copying",
"copyright",
"licence",
"unlicense",
"copyleft",
}
// Percentage returns 0 if no LICENSE, 1 if LICENSE
func (g License) Percentage() (float64, []FileSummary, error) {
files, err := ioutil.ReadDir(g.Dir)
if err != nil {
return 0.0, []FileSummary{}, err
}
for _, file := range files {
name := strings.ToLower(file.Name())
if filepath.Ext(name) == "go" {
continue
}
for i := range licenses {
if strings.HasPrefix(name, licenses[i]) {
return 1.0, []FileSummary{}, nil
}
}
}
return 0.0, []FileSummary{{"", "http://choosealicense.com/", []Error{}}}, nil
}
// Description returns the description of License
func (g License) Description() string {
return "Checks whether your project has a LICENSE file."
}