-
Notifications
You must be signed in to change notification settings - Fork 2
/
license.go
84 lines (74 loc) · 1.93 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
package main
import (
"reflect"
"strings"
"github.com/bitly/go-simplejson"
)
// Licenses holds all unique licenses of the tree
type Licenses map[string]struct{}
// Append appends new license to Licenses
func (licenses Licenses) Append(license string) {
if _, ok := licenses[license]; !ok {
licenses[license] = struct{}{}
}
}
// String convert license map to RPM License string
func (licenses Licenses) String() string {
m := map[string]struct{}{}
for license := range licenses {
if license == "Unlicense" {
continue
}
if strings.Contains(license, " OR ") {
for _, s := range strings.Split(license, " OR ") {
if _, ok := m[s]; !ok {
m[s] = struct{}{}
}
}
} else {
if _, ok := m[license]; !ok {
m[license] = struct{}{}
}
}
}
keys := reflect.ValueOf(m).MapKeys()
strKeys := make([]string, len(keys))
for i := 0; i < len(keys); i++ {
strKeys[i] = keys[i].String()
}
return strings.Join(strKeys, " AND ")
}
// getLicense parse license for package
// three kinds of license expression nowadays:
// 1. String {"license": "MIT"}
// 2. Array {"licenses": [{"type": "MIT", "url": "blabla"}, {"type": "Apache-2.0", "url":"daladala"}]}
// 3. Map {"license": {"type": "MIT", "url": "blabla"}}
// Both 2 and 3 are now deprecated but still in use.
func getLicense(js *simplejson.Json) string {
j := js.Get("license")
r := strings.NewReplacer("(", "", ")", "")
s, e := j.String()
if e == nil {
if !strings.Contains(s, " OR ") {
s = strings.Replace(s, " ", "-", -1)
}
return r.Replace(s)
}
m, e := j.Map()
if e == nil {
s, _ = m["type"].(string)
return s
}
// the only way to check nil value for simplejson
if reflect.ValueOf(j).Elem().Field(0).IsNil() {
jv := js.Get("licenses").MustArray()
a := []string{}
for _, v := range jv {
m := reflect.ValueOf(v).MapIndex(reflect.ValueOf("type")).Interface()
s, _ = m.(string)
a = append(a, s)
}
return strings.Join(a, " OR ")
}
return ""
}