forked from cloudfoundry-attic/bosh-init
/
resolve_dependencies.go
50 lines (43 loc) · 1.1 KB
/
resolve_dependencies.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
package pkg
import (
birelpkg "github.com/cloudfoundry/bosh-init/release/pkg"
)
func ResolveDependencies(pkg *birelpkg.Package) []*birelpkg.Package {
return resolveInner(pkg, []*birelpkg.Package{})
}
func resolveInner(pkg *birelpkg.Package, noFollow []*birelpkg.Package) []*birelpkg.Package {
all := []*birelpkg.Package{}
for _, depPkg := range pkg.Dependencies {
if !contains(all, depPkg) && !contains(noFollow, depPkg) {
all = append(all, depPkg)
tDeps := resolveInner(depPkg, joinUnique(all, noFollow))
for _, tDepPkg := range tDeps {
all = append(all, tDepPkg)
}
}
}
for i, el := range all {
if el == pkg {
all = append(all[:i], all[i+1:]...)
}
}
return all
}
func contains(list []*birelpkg.Package, element *birelpkg.Package) bool {
for _, pkg := range list {
if element == pkg {
return true
}
}
return false
}
func joinUnique(a []*birelpkg.Package, b []*birelpkg.Package) []*birelpkg.Package {
joined := []*birelpkg.Package{}
joined = append(joined, a...)
for _, pkg := range b {
if !contains(a, pkg) {
joined = append(joined, pkg)
}
}
return joined
}