-
Notifications
You must be signed in to change notification settings - Fork 110
/
utils.go
70 lines (62 loc) · 1.52 KB
/
utils.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
package utils
import (
"fmt"
"sort"
"github.com/aquasecurity/go-dep-parser/pkg/types"
"golang.org/x/exp/maps"
)
func UniqueStrings(ss []string) []string {
var results []string
uniq := map[string]struct{}{}
for _, s := range ss {
if _, ok := uniq[s]; ok {
continue
}
results = append(results, s)
uniq[s] = struct{}{}
}
return results
}
func UniqueLibraries(libs []types.Library) []types.Library {
if len(libs) == 0 {
return nil
}
unique := map[string]types.Library{}
for _, lib := range libs {
identifier := fmt.Sprintf("%s@%s", lib.Name, lib.Version)
if l, ok := unique[identifier]; !ok {
unique[identifier] = lib
} else {
// There are times when we get 2 same libraries as root and dev dependencies.
// https://github.com/aquasecurity/trivy/issues/5532
// In these cases, we need to mark the dependency as a root dependency.
if !lib.Dev {
l.Dev = lib.Dev
unique[identifier] = l
}
if len(lib.Locations) > 0 {
// merge locations
l.Locations = append(l.Locations, lib.Locations...)
sort.Sort(l.Locations)
unique[identifier] = l
}
}
}
libSlice := maps.Values(unique)
sort.Sort(types.Libraries(libSlice))
return libSlice
}
func MergeMaps(parent, child map[string]string) map[string]string {
if parent == nil {
return child
}
// Clone parent map to avoid shadow overwrite
newParent := maps.Clone(parent)
for k, v := range child {
newParent[k] = v
}
return newParent
}
func PackageID(name, version string) string {
return fmt.Sprintf("%s@%s", name, version)
}