forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiled_package_repo.go
82 lines (63 loc) · 2.05 KB
/
compiled_package_repo.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
package pkg
import (
"fmt"
"sort"
"strings"
biindex "github.com/cloudfoundry/bosh-cli/index"
birelpkg "github.com/cloudfoundry/bosh-cli/release/pkg"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type CompiledPackageRecord struct {
BlobID string
BlobSHA1 string
}
type CompiledPackageRepo interface {
Save(birelpkg.Compilable, CompiledPackageRecord) error
Find(birelpkg.Compilable) (CompiledPackageRecord, bool, error)
}
type compiledPackageRepo struct {
index biindex.Index
}
func NewCompiledPackageRepo(index biindex.Index) CompiledPackageRepo {
return &compiledPackageRepo{index: index}
}
func (cpr *compiledPackageRepo) Save(pkg birelpkg.Compilable, record CompiledPackageRecord) error {
err := cpr.index.Save(cpr.pkgKey(pkg), record)
if err != nil {
return bosherr.WrapError(err, "Saving compiled package")
}
return nil
}
func (cpr *compiledPackageRepo) Find(pkg birelpkg.Compilable) (CompiledPackageRecord, bool, error) {
var record CompiledPackageRecord
err := cpr.index.Find(cpr.pkgKey(pkg), &record)
if err != nil {
if err == biindex.ErrNotFound {
return record, false, nil
}
return record, false, bosherr.WrapError(err, "Finding compiled package")
}
return record, true, nil
}
type packageToCompiledPackageKey struct {
PackageName string
// Fingerprint of a package captures the sorted names of its dependencies
// (but not the dependencies' fingerprints)
PackageFingerprint string
DependencyKey string
}
func (cpr compiledPackageRepo) pkgKey(pkg birelpkg.Compilable) packageToCompiledPackageKey {
return packageToCompiledPackageKey{
PackageName: pkg.Name(),
PackageFingerprint: pkg.Fingerprint(),
DependencyKey: cpr.convertToDependencyKey(ResolveDependencies(pkg)),
}
}
func (cpr compiledPackageRepo) convertToDependencyKey(packages []birelpkg.Compilable) string {
dependencyKeys := []string{}
for _, pkg := range packages {
dependencyKeys = append(dependencyKeys, fmt.Sprintf("%s:%s", pkg.Name(), pkg.Fingerprint()))
}
sort.Strings(dependencyKeys)
return strings.Join(dependencyKeys, ",")
}